Reputation: 5
I'm new to Rails and have the encountered this problem.
I have a books_controller where I have defined a new action called download:
def download
@book = Book.find(params[:id])
return send_file @book.get_filepath, :type => 'application/pdf', :filename => @book.get_filename
end
My routes.rb is like:
resources :books
match '/books(/download(/:id))', :controller => "books", :action => "download"
I would like to create a URL like /books/download/10 to call the download action.
I'm not sure how I can create that link. I have tried reading the Rails Guide on routing, but am very confused by it.
I have tried
<td><%= link_to books_download_path(book) %></td>
and it clearly doesn't work.
undefined method `books_download_path' for #<#<Class:0x682ac40>:0x482cad8>
I'd appreciate any help on this.
Thanks
P.S. Maybe /books/10/download makes more sense than /books/download/10
index.html.erb
<table>
<% @recent_books.each do |book| %>
<tr>
<!-- <td><%= image_tag(book.get_thumbnail) %></td> -->
<td><%= truncate(book.get_title, :length => 30) %></td>
<td><%= book.get_author %></td>
<td><%= book.get_summary %></td>
<td><%= truncate(book.get_filename, :length => 30) %></td>
<!-- <td><%= link_to 'Download', book %></td> -->
<td><%= link_to "Download", download_book_path(book) %></td>
</tr>
<% end %>
<table>
routes.rb
Caliberator::Application.routes.draw do resources :authors
resources :books do
get :download, :on => :member, :to => 'books#download'
end
end
Upvotes: 0
Views: 227
Reputation: 50057
For rails 3, you should write:
resources :books do
member do
get 'download'
end
end
Upvotes: 0
Reputation: 5237
You have to do it like this :
<%= link_to "Download", download_books_path + "/#{book.id}" %>
Upvotes: 0
Reputation: 3915
For Rails 3, try this
resources :books do
get :download, :on => :member, :to => 'books#download'
end
Now, in your views, you can use.
<%= link_to 'Download', download_book_path(book) %>
This will generate books/10/download
type links.
Upvotes: 2
Reputation: 47462
Try following
match '/books(/download(/:id))', :controller => "books", :action => "download", :as => "books_download"
Ref:- naming_routes
Upvotes: 0