Forum: Ruby on Rails beginner: help using <select> drop down list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike Y. (Guest)
on 2006-01-06 17:11
Hi, first time posting here. I'm in the process of learning ruby and the
rails framework and i've come to a road block. I started off by using
the Onlamp tutorial online and got a test database and all of the
generated scaffold pages.  I am currently working on a drop down box
using the select tag.  I figured out how to get values from a 'name'
column in my Test table, i'm using mysql for database access.

The problem i'm stuck at now is that I want to be able to select a name
from the list and click a button and have it forward to the 'show.rhtml'
page for the selected name.  I'm trying to reformat the 'list.rhtml'
page that was created by the scaffold generator, so that there is only
this drop down box, instead of having all the rows in the table show up
with all the columns and data.

If anyone can help me that would be great. i've googled all day and
found no examples to help me with this.

thanks
Jan P. (Guest)
on 2006-01-06 19:21
(Received via mailing list)
Hi, Mike,

please try this (fitting it to your needs):

In your controllers list method load all items (in my example books)
into an instance variable:

@books = Book.find(:all)

In your list-view display these books in a select box with:

<%= start_form_tag :action => 'show_book' %>
  <%= collection_select(:book, :id, @books, :id, :title) %>
  <%= submit_tag 'Show' %>
<%= end_form_tag %>

after submitting with the show button you can catch a parameter
params[:book][:id] and use this to find the book that you would like to
display.

def show_book
  redirect_to(:action => "show", :id => params[:book][:id])
end

Regards
Jan
Mike Y. (Guest)
on 2006-01-06 20:15
Thanks for the help, I follow what you are saying.  I'm gonna give it a
try and see how it goes.

Thanks again
This topic is locked and can not be replied to.