I have this route:
resources :projects do
resources :services
resources :contacts
resources :title_abstracts
resources :parcels
resources :leases
resources :documents
end
this is in my Projects View to add a new Contact to the Project:
new_project_contact_path(@project)
The url produced is: http://localhost:3000/projects/15/contacts/new
And the Contacts Controller:
def new
@project = Project.find(params[:project_id])
@contact = @project.contacts.build
end
def create
@project = Project.find(params[:project_id])
@contact = @project.contacts.build(params[:contact])
@contact.save
redirect_to project_path
end
But I get the following error:
Couldn't find Project with 'id'=
What am I doing wrong? How should I test this?
In your routes.rb you nested :contacts
in :projects
.
but when you use simple_form_for
, you cannot implicitly declare <%= simple_form_for @contact do |f| %>
. You should pass the url:{controller: :contacts, action: :create, project_id:@project.id}
as an argument along with the new @contact
instance to simple_form_for
. so that params[:project_id]
will be available in your controller.
If you really wish to use <%= simple_form_for @contact do |f| %>
, you need to add
resources :contacts
to your routes.rb
file but you need to optimize your controller action code for desired routing. I think it would help you.Thank you.