简体   繁体   English

FORM_TAG(Rails):如何将选定内容添加到选项中?

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

I've a "form_tag" form in rails. 我在rails中有一个“form_tag”表单。

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 我想要在另一个变量@ object.id中选择对象的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") (通常情况下,我用collection_select来解决这个问题。但据我所知,这与“form_tag”没有合作 - 只有“form_for”)

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

By default, post.person_id is the selected option. 默认情况下,post.person_id是选定的选项。 Specify :selected => value to use a different selection or :selected => nil to leave all options unselected. 指定:selected => value以使用其他选择或:selected => nil以取消选中所有选项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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