# Map.resources + path?

**URL:** <https://www.ruby-forum.com/t/map-resources-path/137458>\
**Category:** Rails\
**Created:** [May 13, 2008, 11:06am UTC](https://www.ruby-forum.com/t/map-resources-path/137458 "2008-05-13T11:06:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 13, 2008, 11:06am UTC](https://www.ruby-forum.com/t/map-resources-path/137458/1 "2008-05-13T11:06:05Z")

</div>

REST/path question…

routes.rb  
map.resources :continent do |continent|  
continent.resources :land do |land|  
land.resources :destination  
end  
end

Show.rhtml of land(countrie) controller, to show the (belongs\_to)  
destinations

## Destinations

\<% @Destination.each do |destination| %\>- \<%= link\_to destination.naam, continent\_land\_destination\_path(@land.continent, @land, destinations)%\>
\<% end %\>

problem:  
On my germany-page  
[http://www.goedkoop-vliegen.nu/werelddeel/europa/land/duitsland](http://www.goedkoop-vliegen.nu/werelddeel/europa/land/duitsland)

It generates all my destinations…in this url structure…(atlanta)

[http://www.goedkoop-vliegen.nu/werelddeel/europa/land/duitsland/bestemming/atlanta](http://www.goedkoop-vliegen.nu/werelddeel/europa/land/duitsland/bestemming/atlanta)

The url isn’t mapping (example: atlanta) to

[http://www.goedkoop-vliegen.nu/werelddeel/noord-amerika/land/verenigde-staten/bestemming/atlanta](http://www.goedkoop-vliegen.nu/werelddeel/noord-amerika/land/verenigde-staten/bestemming/atlanta)

Which parameters must i define in

link\_to destination.naam,  
continent\_land\_destination\_path(paramaters,parameters ect) ?

Grtz…remco

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/map-resources-path/137458/2 "2008-05-13T16:37:17Z")

</div>

> \<% @Destination.each do |destination| %\>

shouldn’t that be

\<% @land.destinations.each do |destination| %\>

?

I guess Rails interprets @Destination as Destination and loops through  
_all_ Destination records. It’s hard to say without the controller  
method,  
but @land doesn’t seem to be linked to destination in your code.

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/map-resources-path/137458/3 "2008-05-13T16:37:52Z")

</div>

First, I’d suggest using lower case unless you’re using a class name  
(@Destination =\> @destination).

How did you set @destination? It would appear that your controller  
should have something like:

@land = Land.find(…)  
@destinations = @land.destinations.find(:all, :order=\>:naam)

Otherwise, your view looks okay except for a possible typo (check the  
link\_to call, you refer to destinations but have not defined it):

## Destinations in \<%= h destination.naam %\>

\<% @destinations.each do |destination| %\>- \<%= link\_to destination.naam, continent\_land\_destination\_path(@land.continent, @land, destination)%\>
\<% end %\>

On May 13, 5:06 am, Remco Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 14, 2008, 9:10am UTC](https://www.ruby-forum.com/t/map-resources-path/137458/4 "2008-05-14T09:10:14Z")

</div>

AndyV wrote:

> First, I’d suggest using lower case unless you’re using a class name  
> (@Destination =\> @destination).
> 
> How did you set @destination? It would appear that your controller  
> should have something like:
> 
> @land = Land.find(…)  
> @destinations = @land.destinations.find(:all, :order=\>:naam)
> 
> Otherwise, your view looks okay except for a possible typo (check the  
> link\_to call, you refer to destinations but have not defined it):
> 
> ## Destinations in \<%= h destination.naam %\>
> 
> \<% @destinations.each do |destination| %\>- \<%= link\_to destination.naam, continent\_land\_destination\_path(@land.continent, @land, destination)%\>
> \<% end %\>
> 
> On May 13, 5:06 am, Remco Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi…

The url structure is not good if i use this code…check \>  
[http://www.goedkoop-vliegen.nu/werelddeel/afrika/land/kenia](http://www.goedkoop-vliegen.nu/werelddeel/afrika/land/kenia)

The continent(werelddeel)and land(countrie) is not changing…

The code for continents works fine for continents…  
[http://www.goedkoop-vliegen.nu/werelddeel](http://www.goedkoop-vliegen.nu/werelddeel)

## Landen

\<% @landen.each do |land| %\>- \<%= link\_to land.naam, continent\_land\_path(land.continent, land)%\>
\<% end %\>

When i try this

## destinations

\<% @destinations.each do |destination| %\>- \<%= link\_to land.naam, continent\_land\_destination\_path(land.continent, land, destinations)%\>
\<% end %\>

I get the error message

undefined local variable or method `land’ for  
#ActionView::Base:0xb79f6e44

This is my setup:

map.resources :continent do |continent |  
continent .resources :land do |land|  
land.resources :destination  
end  
end

class Destination \< ActiveRecord::Base  
has\_many :land, :foreign\_key =\> “landcode”  
set\_table\_name “bestemmingen”  
set\_primary\_key “bestemming\_id”  
has\_permalink :stad\_zoeknaam

def to\_param  
stad\_zoeknaam  
end

end

class Land \< ActiveRecord::Base  
belongs\_to :continent, :foreign\_key =\> “werelddeel\_id”  
has\_many :destination, :foreign\_key =\> “landcode”  
set\_table\_name “landen”  
set\_primary\_key “landcode”

has\_permalink :land\_zoeknaam

def to\_param  
land\_zoeknaam  
end  
end

Grtz…remco

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 14, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/map-resources-path/137458/5 "2008-05-14T14:50:06Z")

</div>

I was assuming that you were displaying the destinations for a  
specific land. Your controller should be setting an instance variable  
(@land) and you should be using that instance variable in the link\_to  
call (as shown above). In the code that you’ve shown in your last  
post you are using a local variable (land) rather than an instance  
variable (@land).

Two other observations. First, I’m not familiar with the language but  
it appears from your posts that landen is the plural of land. If so,  
your nested routes might be more natural as:

map.resources :continent do |continent |  
continent .resources :landen do |land|  
land.resources :destination  
end  
end

Second, there is a growing sentiment toward avoiding nesting resources  
too deeply. The primary argument in favor of nesting routes is to  
provide the proper context for some ‘belongs\_to’ (child)  
relationship. Nesting beyond a certain level becomes a burden to the  
developer (the xxx\_yyy\_zzz\_aaa\_path statements are too long) and the  
‘pretty urls’ are pretty only in the sense of being ‘pretty long’.  
All that to say you might consider breaking up your nesting if you get  
tired of all that keying:

map.resources :continent do |continent |  
continent.resources :landen  
end

map.resources :landen do |land|  
land.resources :destination  
end

You may be fine where you are right now, but I would not recommend  
going any deeper with your nesting.

On May 14, 3:10 am, Remco Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 15, 2008, 9:05am UTC](https://www.ruby-forum.com/t/map-resources-path/137458/6 "2008-05-15T09:05:57Z")

</div>

AndyV wrote:

> I was assuming that you were displaying the destinations for a  
> specific land. Your controller should be setting an instance variable  
> (@land) and you should be using that instance variable in the link\_to  
> call (as shown above). In the code that you’ve shown in your last  
> post you are using a local variable (land) rather than an instance  
> variable (@land).
> 
> Two other observations. First, I’m not familiar with the language but  
> it appears from your posts that landen is the plural of land. If so,  
> your nested routes might be more natural as:
> 
> map.resources :continent do |continent |  
> continent .resources :landen do |land|  
> land.resources :destination  
> end  
> end
> 
> Second, there is a growing sentiment toward avoiding nesting resources  
> too deeply. The primary argument in favor of nesting routes is to  
> provide the proper context for some ‘belongs\_to’ (child)  
> relationship. Nesting beyond a certain level becomes a burden to the  
> developer (the xxx\_yyy\_zzz\_aaa\_path statements are too long) and the  
> ‘pretty urls’ are pretty only in the sense of being ‘pretty long’.  
> All that to say you might consider breaking up your nesting if you get  
> tired of all that keying:
> 
> map.resources :continent do |continent |  
> continent.resources :landen  
> end
> 
> map.resources :landen do |land|  
> land.resources :destination  
> end
> 
> You may be fine where you are right now, but I would not recommend  
> going any deeper with your nesting.
> 
> On May 14, 3:10 am, Remco Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

He Andy…

You’re reply (thanks!!) and analysing my log file…put me in the  
wright direction…and it works now…this is my final code.

Land-Controller:  
@destination= destination.find\_all\_by\_landcode(@land.landcode)

Land-View:  
\<% @destinations.each do |destination| %\>

- \<%= link\_to destination.naam, continent\_land\_destination\_path(@land.continent, @land, destination)%\>
\<% end %\>

Land-Model:

class Land \< ActiveRecord::Base  
belongs\_to :continent, :foreign\_key =\> “werelddeel\_id”  
has\_many :destination, :foreign\_key =\> “landcode”  
set\_table\_name “landen”  
set\_primary\_key “landcode”

has\_permalink :land\_zoeknaam

def to\_param  
land\_zoeknaam  
end  
end
