Path helpers in a loop
I have a Foo that :has_many
Bars. GET Foo#index
shows all of开发者_开发知识库 the Bars. View looks like this:
<% @foos.each do |foo| %>
<% foo.bars.each do |bar| %>
<%= link_to 'Download', download_bar_path %>
<%= link_to 'New', new_bar_path( :foo => foo.id ) %>
<% end %>
<% end %>
There is a def download
in Bars controller and a route:
resources :bars do
member do
get 'download'
end
end
rake routes
shows
download_bar GET /bars/:id/download(.:format) {:action=>"download", :controller=>"bars"}
and URL /bars/1/download
really works, but the first link in the view (download_bar_path
) doesn't. It says No route matches {:action=>"download", :controller=>"bars"}
.
What can be the problem?
<% @foos.each do |foo| %>
<% foo.bars.each do |bar| %>
<%= link_to 'Download', [:download, bar] %>
<%= link_to 'New', [:new, :bar] %>
<% end %>
<% end %>
You didn't specified the bar to download, you need to add it by changing this line
<%= link_to 'Download', download_bar_path(bar) %>
精彩评论