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
It generates all my destinations…in this url structure…(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
Which parameters must i define in
link_to destination.naam,
continent_land_destination_path(paramaters,parameters ect) ?
Grtz…remco
<% @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.
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. [email protected]
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. [email protected]
Hi…
The url structure is not good if i use this code…check >
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
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
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. [email protected]
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. [email protected]
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