Tuesday, January 01, 2008

Ruby on Rails 2.0: link_to for nested resources

If you specify nested resources in routes.rb such as

map.resources :daily_contents do |daily_content|
daily_content.resources :vocabularies

you have to specify a parameter for link_to like this:

<% for vocabulary in @daily_content.vocabularies %>
<%= link_to 'Show', [@daily_content, vocabulary] %>
<% end %>

The generated URL is like this:


Moreover, if @daily_content.entities is just an array, which happens to contain an object of type, Vocabulary, the following works as well:

<% for entity in @daily_content.entities %>
<%= link_to 'Show', [@daily_content, entity] %>
<% end %>

Again the generated URL is same as above like:


No comments: