简体   繁体   中英

How to default the option list when editing in rails

I have a standard rails controller that I would like to edit and be able to keep the current option list selected.

So I have a form that the user has filled out.
Example: name(textfield) & state(option list)

When the user clicks the edit button, it goes to the generated edit page. The user has previously selected a name and a state. The name textfield comes back populated, but the state defaults to the first one in the list.
How can I get my edit method in the controller to pass in the users selected state to the edit view page?

我不确定您如何定义选项列表,但可以使用options_from_collection_for_select并将proc传递给它来有条件地选择选项。

这应该做的工作

<%= f.select :project_id, @list, :selected => "user_selected_value"] %>

You may be having trouble mass-assigning the field associated with the state drop-down listing. Make sure attr_accessible on that attribute is properly set in the model or you will have that attribute assignment ignored when using methods like new , create , attributes= or update_attributes .

If that's not the case, then you may be having conflict between string keys in your options and an integer value in the model. Remember that 2 is not the same thing as "2" so you should be certain that the keys for your drop-down match the type in question.

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