简体   繁体   English

Select2 multiple rails form value format?

[英]Select2 multiple rails form value format?

I'm using Select2 3.5.2 with a Rails 2.3 app using Postgres and have a form field with multiple: true 我正在使用带有Postgres的Rails 2.3应用程序的Select2 3.5.2 ,并且有一个带有multiple: true的表单字段multiple: true

$('.select2').select2()

<%= f.collection_select :user_ids, User.all, :id, :name, class: 'select2', multiple: true, %>

When saving, the database is populated with (for example): 保存时,数据库填充(例如):

--- 
- ""
- "4"
- "7"

But when editing the form, the values do not populate the select2. 但是在编辑表单时,值不会填充select2。 I have tried passing multiple kinds of values into the select to try to populate it, but no luck. 我已经尝试将多种值传递给select以尝试填充它,但没有运气。 I've tried: 我试过了:

value: "--- \n- ""\n- "4"\n- "7""
value: 4,7
value: [4,7]
value: ['4', '7']
value: "[\"4\", \"7\"]"

What value does it expect so that it can be populated with existing values? 它期望什么价值,以便它可以填充现有的价值?

Ok, I figured it out. 好的,我明白了。 I was putting the value in the wrong place. 我把价值放在了错误的地方。 Instead of: 代替:

<%= f.collection_select :user_ids, User.all, :id, :name, class: 'select2', multiple: true, value: [1] %>

I needed: 我需要:

<%= f.select :user_ids, options_from_collection_for_select(User.all, :id, :name, params[:user_ids] || [1,2]), class: 'select2', multiple: true %>

At least, that worked for me. 至少,这对我有用。

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

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