简体   繁体   中英

FORM_TAG (Rails): how to add selected to an option?

I've a "form_tag" form in rails.

Now I've the problem, that I'm loading some options from my DB to a select tag.

<%= select("get", "object_id", Object.all.collect {|p| [p.name, p.id, {:'data-email' => p.email}] }) %>

I've the ID of the object I want to get selected in another variable @object.id

(normally, I solve that with collection_select. But that's not working together with "form_tag" as far as I can see - only with "form_for")

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

By default, post.person_id is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.

select("get", "object_id", Object.all.collect {|p| [p.name, p.id, {:'data-email' => p.email}] }, { :selected => @object.id })

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