Quizically
Quizically

Reputation: 113

How do I obtain a selected value in Ruby on Rails 2.3.8?

My select_tag is as follows.

<%= select_tag "group", options_from_collection_for_select(@groups, "id", "gname") %>

How do I obtain the selected value in my controller?

Upvotes: 0

Views: 120

Answers (3)

jvnill
jvnill

Reputation: 29599

That should be params[:group] in your controller.

Upvotes: 0

sjain
sjain

Reputation: 23354

Use square brackets.

select_tag "group[]", options_for ....

Note the []. Rails will then store this as {"group" => [one option for each form]}.

If it's important to know which select provided which value, you can nest them, so

select_tag "group[bob]", ... 

will provide {"group" => {"bob" => selected_option}}.

Basically, [] stores it in an array, and [key] stores it in a hash with that key.

Then in controller, you can use as:

params["group"], which should be an array of the various selects on the page.

Upvotes: 1

tom
tom

Reputation: 718

Try puts params and check the your console to see values sent to the controller.

Upvotes: 0

Related Questions