mardi 8 décembre 2015

Rails "No route matches" error

I'm getting the following error in my Rails app:

No route matches {:action=>"edit", :controller=>"customers", :customer_id=>1}

Following is the jobs view line to link to edit customer details (customer has many jobs, but no nested resources):

<%= link_to job.customer_id, edit_customer_path(:customer_id => job.customer_id) %>

Following is edit definition in the controller:

def edit if params[:customer_id] @customer = Customer.find(params[:customer_id])
elsif params[:id] @customer = Customer.find(params[:id]) end respond_to do |format| format.html # edit.html.erb format.json { render json: @customer } end end

rake routes gives the following:

edit_customer GET /customers/:id/edit(.:format) customers#edit

NOTE:

If I change the view to:

<%= link_to job.customer_id, edit_customer_path(:id => job.customer_id) %>

then I get the same error but with ":id=nil" (i.e. with no value passed at all):

No route matches {:action=>"edit", :controller=>"customers", :id=>nil}

I am a little new to this, but can someone please explain what is going on?

Thanks!

Aucun commentaire:

Enregistrer un commentaire