Forum: Ruby on Rails select helper method

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.
6738e5c4f17822f7e163ba938344755d?d=identicon&s=25 last resort (Guest)
on 2006-03-04 23:50
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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-05 01:41
(Received via mailing list)
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.
6738e5c4f17822f7e163ba938344755d?d=identicon&s=25 last resort (Guest)
on 2006-03-05 10:58
Mark Reginald James 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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-05 12:15
(Received via mailing list)
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 Development 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.
This topic is locked and can not be replied to.