简体   繁体   中英

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?

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.

尝试puts params并检查控制台以查看发送到控制器的值。

那应该是控制器中的params[:group]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM