0

when I visit 'company/new', then I get the following error:

undefined method `companies_path'
Extracted source (around line #1):
1: <%= form_for(@company) do |f| %>

But when I visit 'company/1/edit' (which uses the same form) everything works fine. This is the company controller for 'new' and 'edit':

def new
  @company = Company.new
end
def edit
  @company = Company.find(params[:id])
end

And this is (part of) the form:

<%= form_for(@company) do |f| %>
<!-- Show errors -->
<%= render('layouts/form_errors', :object => @company) %>

I really don't understand the error message, because 'companies_path' is not being used in the code?

Update: here is the routes.rb:

  get "users_dashboard/show"
  get "login" => "sessions#new", :as => "login" 
  get "logout" => "sessions#destroy", :as => "logout"

  resources :company
  resources :relations
  resources :activities
  resources :contacts
  resources :notes
  resources :tasks
  resources :users
  resources :sessions

  get "site/index"
  get "site/features"
  get "site/dashboard"

  root :to => 'users_dashboard#show'

And here is the company model:

class Company < ActiveRecord::Base
has_many :users
has_many :relations
has_many :contacts, :through => :relations
has_many :notes, :through => :contacts
has_many :tasks, :through => :contacts
has_one :subscription

accepts_nested_attributes_for :subscription

attr_accessible :name, :address1, :address2, :zipcode, :city, :country, :email,      :website, :telephone, :twitter, :linkedin, :code

validates       :name, :address1, :zipcode, :city, :country, :code, presence: true
validates_length_of :code, :maximum => 3

end

John
  • 6,404
  • 14
  • 54
  • 106

1 Answers1

2

You should change

resources :company

to

resources :companies
lucapette
  • 20,564
  • 6
  • 65
  • 59
  • Then it gives a "uninitialized constant CompaniesController" error. And why would the form then work with an edit and not with a new, while requesting the same _form partial? – John Nov 29 '11 at 11:05
  • It is correct. For some reasons you're not following the Rails conventions for handling restful resources. The edit works because, by convention, it has the same route for singular and plural resources. See http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions for further information. How have you created the company model? – lucapette Nov 29 '11 at 11:56
  • The Company model should be pretty straightforward. It seems the controller is calling something plural which it should not do. – John Nov 30 '11 at 07:38
  • Solved it! The company controller was still singular, when changed to CompaniesController (and also the file name itself) it now works. Stupid mistake! – John Nov 30 '11 at 07:48