views:

721

answers:

2

I'm having an issue with my routes and the name of the path it generates. Or I'm naming it wrong in the view or something... not totally sure. My relationships in my models are as follows:

class Client < ActiveRecord::Base
  has_many :users
  has_many :elements, :through => :users 
end

class Element < ActiveRecord::Base
  belongs_to :user
end

My routes are like:

map.resources :elements
map.resources :clients, :has_many => :elements

And in my view I have:

<%= link_to element.name, client_element_url %>

But the error I get is:

    edit_client_element_url failed to generate from {:action=>"edit", :controller=>"elements"} - you may have ambiguous routes, or you may need to supply additional parameters for this route.  content_url has the following required parameters: ["clients", :client_id, "elements", :id, "edit"] - are they all satisfied?

I'm not sure how to proceed, am I missing something that is right in my face?

+2  A: 

You need to pass in the element and the client to the url method:

<%= link_to element.name, client_element_url(element.client, element)

This way it knows which client_id and element_id to use in the route

/clients/:client_id/elements/:element_id
erik
In fact, the end of the error message is telling you that problem, albeit in a less-than-straightforward manner: "["clients", :client_id, "elements", :id, "edit"] - are they all satisfied?" Certainly "clients, "elements," and "edit" are all "satisfied" as they're constants. That leaves :client_id and :id, which need to be filled in as erik suggests.
James A. Rosen
There's no need to do so, but if you feel more comfortable with the explicit version, you can do `client_element_url(:client_id => element.client, :id => element)`.
James A. Rosen
A: 

Thanks @Erik you did solve it, I was wondering though, why doenst it assume the current elements id? Why do I have to specify it? I was able to say <%= element.name %> and it says the correct name of the element.

Anyhow, i'll figure that out at some point, thanks guys! Thanks @Gaius.

Joseph Silvashy