Select helper method


#1

Hey

I’m having problems understanding the select helper. Here is my code:


DB table: genres
id
name
descripition


In the controller:

def index()
    @genres = Genre.find_all.map {|u|[u.name, u.id]}
    @id = params['genre']
end

In the view:

<%
@genre = Genre.new
@genre.id = @id
%>

<%= start_form_tag :action => ‘index’ %>
<%=select(:genre, :id, @genres)
%>
<%= submit_tag “show genre”%>
<%= end_form_tag %>


What I’m trying to do is to select the option that was selected when the
form was submitted back to the server…
The value I’m getting back in the controller after submit is ‘id2’
whereas I would like to just get ‘2’.
Is it possible?


#2

last resort wrote:

@genre = Genre.new
@genre.id = @id
%>

What I’m trying to do is to select the option that was selected when the
form was submitted back to the server…
The value I’m getting back in the controller after submit is ‘id2’
whereas I would like to just get ‘2’.
Is it possible?

@id = params[‘genre’][‘id’] is what you want.

The idiomatic Rails way is however to move the creation of @genre
into the controller:
@genre = Genre.new( params[:genre] )


We develop, watch us RoR, in numbers too big to ignore.


#3

Mark Reginald J. wrote:

@id = params[‘genre’][‘id’] is what you want.

The idiomatic Rails way is however to move the creation of @genre
into the controller:
@genre = Genre.new( params[:genre] )

Thank you, works perfectly. Do you know where I might find more info on
these kind of things? I have searched the net like crazy.

I also have the two official rails books.


#4

last resort wrote:

Thank you, works perfectly. Do you know where I might find more info on
these kind of things? I have searched the net like crazy.

I also have the two official rails books.

The Depot tutorial and Chapter 14 of “Agile Web D. with Rails”
(pragmaticprogrammer.com/titles/rails/) would be a good introduction.

Browsing the source of the Typo blog would also be instructive:
http://www.typosphere.org/trac/browser/trunk


We develop, watch us RoR, in numbers too big to ignore.