# Help with Routes

**URL:** <https://www.ruby-forum.com/t/help-with-routes/133212>\
**Category:** Rails\
**Created:** [March 28, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212 "2008-03-28T19:51:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 28, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212/1 "2008-03-28T19:51:40Z")

</div>

I have the following models

entity  
has\_many :locations  
has\_many :sites, :through =\> :locations

location  
belongs\_to :entity  
belongs\_to :site

site  
has\_many :locations  
has\_many :entities, :through =\> :locations

AND routes:

map.resources :entities do |entity|  
entity.resources :locations,  
:collection  
end

What I wish to do is to display all of the locations for a given entity.  
I have modified locations\_controller to this:

# Limit locations list to just the parent entity

before\_filter :load\_entity

# GET /locations

# GET /locations.xml

def index  
@locations = @entity.locations.find(:all)  
…  
private

def load\_entity  
@entity = Entity.find parms[:entity\_id]  
end

And in views/entities/index.html.erb when I have this:

```
<td>
  <%= link_to 'Locations', entity_location_path -%>
</td>

```

Then I get this:

entity\_location\_url failed to generate from  
{:controller=\>“locations”, :action=\>“show”}

- you may have ambiguous routes, or you may need to supply  
additional parameters for this route. content\_url has  
the following required parameters:  
[“entities”, :entity\_id, “locations”, :id]
- are they all satisfied?

If I have this in index.html.erb instead:

```
<td>
  <%= link_to 'Locations', entity_location_path(entity),
         :method => 'index' -%>
</td>

```

Then I get this:

entity\_location\_url failed to generate from  
{:controller=\>“locations”, :entity\_id=\>#\<Entity id: 1,  
entity\_name: “my first client”, entity\_legal\_name: “My First Client  
Ltd.”,  
entity\_legal\_form: “CORP”, created\_at: “2008-03-25 22:41:43”,  
updated\_at: “2008-03-25 22:41:43”\>, :action=\>“show”},  
expected:  
{:controller=\>“locations”, :action=\>“show”},  
diff:  
{:entity\_id=\>#\<Entity id: 1, entity\_name: “my first client”,  
entity\_legal\_name: “My First Client Ltd.”, entity\_legal\_form:  
“CORP”,  
created\_at: “2008-03-25 22:41:43”, updated\_at: “2008-03-25  
22:41:43”\>}

Which sends me back, as far as I can tell, to the first form of the  
call. Can someone straighten me out with respect to the proper link\_to  
call to use?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 28, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212/2 "2008-03-28T20:32:24Z")

</div>

James B. wrote:

> map.resources :entities do |entity|  
> entity.resources :locations,  
> :collection  
> end

This incudes the route:  
entity\_location GET /entities/:entity\_id/locations/:id  
{:controller=\>“locations”,  
:action=\>“show”}

So:

> ```
> <td>
> <%= link_to 'Locations', entity_location_path -%>
> </td>
> 
> ```
> 
> Then I get this:
> 
> entity\_location\_url failed to generate from  
> {:controller=\>“locations”, :action=\>“show”}
> 
> - you may have ambiguous routes, or you may need to supply  
> additional parameters for this route. content\_url has  
> the following required parameters:  
> [“entities”, :entity\_id, “locations”, :id]
> - are they all satisfied?

This is saying that the route is missing both :entity\_id and :id to  
complete a route match.

> If I have this in index.html.erb instead:
> 
> ```
> <td>
> <%= link_to 'Locations', entity_location_path(entity),
> :method => 'index' -%>
> </td>
> 
> ```
> 
> Then I get this:
> 
> entity\_location\_url failed to generate from  
> {:controller=\>“locations”, :entity\_id=\>#\<Entity id: 1,

Here, you can see that you are now supplying :entity\_id but you still  
need to supply the :id parameter (which is the particular location for  
this entity you want to GET).

Since you are clearly after the index method, then you need to pluralise  
the “location” part of the helper name:

link\_to ‘Locations’, entity\_locations\_path(entity)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 28, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212/3 "2008-03-28T21:23:10Z")

</div>

Mark B. wrote:

> Here, you can see that you are now supplying :entity\_id but you still  
> need to supply the :id parameter (which is the particular location for  
> this entity you want to GET).
> 
> Since you are clearly after the index method, then you need to pluralise  
> the “location” part of the helper name:
> 
> link\_to ‘Locations’, entity\_locations\_path(entity)

Thank you. I am now past this point and running into another problem.  
In the locations\_controller I have this for my new method:

def new

```
@location = @entity.build_location

respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @location }
end

```

end

However, when I call this from views/locations/index.html.erb

\<%= link\_to ‘New location’, new\_entity\_location\_path( params[:entity\_id]  
) %\>

Then I get this:

undefined method `build\_location’ for #Entity:0x4df2ad8

I was under the impression that the build\_location method was  
automatically generated in consequence of the have\_many attribute in  
entity.rb.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 28, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212/4 "2008-03-28T22:38:48Z")

</div>

James B. wrote:

> I infer that locations is an array.

It is an association object which includes the Enumerable module and so  
behaves like an array.

For a has\_many/belongs\_to relationship, you can add new records from  
both sides of the relationship. In your case:

class Entity  
has\_many :locations

class Location  
belongs\_to :entity

You can add a new location to an entity’s locations:

e = Entity.find :first  
e.locations.build …  
e.locations.create …

And you can create a new entity that a location will point to:

l = Location.find :first  
l.build\_entity …  
l.create\_entity …

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 28, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/help-with-routes/133212/5 "2008-03-28T21:27:00Z")

</div>

But, playing around in the console disabused me of that notion. What  
methods I have for locations in Entity are:

- location\_ids
- location\_ids=
- locations
- locations=

I infer that locations is an array.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 29, 2008, 3:31am UTC](https://www.ruby-forum.com/t/help-with-routes/133212/6 "2008-03-29T03:31:30Z")

</div>

Mark B. wrote:

> It is an association object which includes the Enumerable module and so  
> behaves like an array.
> 
> For a has\_many/belongs\_to relationship, you can add new records from  
> both sides of the relationship. In your case:
> 
> class Entity  
> has\_many :locations
> 
> class Location  
> belongs\_to :entity
> 
> You can add a new location to an entity’s locations:
> 
> e = Entity.find :first  
> e.locations.build …  
> e.locations.create …

This is the bit I was missing. I was trying to use e.build\_location.
