Reputation: 14574
Man there are a lot of these questions but so far I have not found my particular problem.
I have this custom route showing when I run rake routes (this is not how I plan to have it long term just to test the routing out).
# routes.rb
match 'books/:id/file/:name' => 'home#download', :as => :download_book, :via => :get
# rake routes
download_book GET /book/:id/file/:name(.:format) {:controller=>"books", :action=>"download"}
In the view I have
= link_to name, download_book_path(@book.id, name: name)
And of course my HomeController
, which is already serving another simple custom route so I know it's working, has a download
action defined.
So the million dollar question is what is up with this error?
ActionView::Template::Error (No route matches {:controller=>"home", :action=>"download", :name=>"test.zip", :id=>"fcd5c87aef84874eec8f08cc313da85c"}):
14: %p
15: %b Published:
16: - @book.attachments.keys.each do |name|
17: = link_to name, download_book_path(@book.id, name: name)
Upvotes: 1
Views: 734
Reputation: 34072
The problem is the dot in your name
variable, which doesn't work out of the box in routes as it conflicts with the standard (.:format)
route section, as you see in your rake routes
call.
The solution can be found in the rails guide on routing. To quote:
By default dynamic segments don’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment add a constraint which overrides this – for example :id => /[^/]+/ allows anything except a slash.
So if you do like they say here and change the constraint to allow dots, it should work:
match ..., :constraints => { :name => /[^\/]+/ }
Side note, in your example you're passing name
in a hash, which would be interpreted as query string variables.
# Rather it should simply be
= link_to name, download_book_path(@book.id, name)
Upvotes: 3