1
votes

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!

1
in rails console try app.edit_customer_path(1)Rajarshi Das
Thanks, but I am trying to pass a parameter, not a hardcoded value. Or have I misunderstood what you are saying?omar
Note that if I add edit_customer_path(1) in the view, it works, the edit page comes up. So something is wrong with the parameter being passed, but I can't figure out what...omar
ok then edit_customer_path(job.try(:customer)) unless job.try(:customer).blank? do it in your view hope it will workRajarshi Das

1 Answers

1
votes

Update

Try writing your path like this

edit_customer_path(job.customer) if job.customer

In your routes, specify one parameter for customers.

resources :customers, param: :customer_id

so you always know what the id is going to look like. (there is some trickery required to make this work all the way through resources).

Also, another potential issue (which has caught me out a few times) is that I have instantiated on the same page a blank instance of the same class I am trying to route to. eg @customer = Customer.new, which the link to is looking for, but doesn't find an id as the record hasn't been saved.

I find named routes a hassle, and much prefer using polymorphic urls as they degrade gracefully.

In your case it would mean something like this.

link_to 'whev', [:edit, @customer]