A model named ‘book’ with attributes ‘name’ and ‘id’ is given. How can i use this collection select to call the show-action of a certain book? The one code mentioned below returns the following error message:
Couldn’t find Book with ID=book_id
<% form_tag(book_path(:book_id)), :method => :get do %> <p> <%= label(:book, :id, 'Show Book:') %> <%= @books = Books.find(:all, :order => :name) collection_select(:book, :id, @books, :id, :name) %> </p> <p> <%= submit_tag 'Go' %> </p> <% end %>
book_pathis generated once only, for the form tag itself. It won’t be updated whenever your selection changes.When you submit that form, it’s going to request the following URL:
Since your
book_paththinks book_id is the ID number you wanted, it tries to look that up. You could do what you want you by changing the code in your controller from:to:
But it kind of smells bad so be warned.