在collection_select rails中选择多个选项

时间:2022-10-28 21:55:13

I know how to put together a simple select box that takes its values from a model

我知道如何组合一个简单的选择框,从模型中获取其值

<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %>

My question is how do i allow a user to select multiple options and then store them in the model. I know i need to use

我的问题是如何让用户选择多个选项,然后将它们存储在模型中。我知道我需要使用

:multiple => true

But unsure on the syntax

但不确定语法

Usually for multiple entries to a model i would use accepts_nested_attributes_for but am i correct in thinking i don't need to for this example?

通常对于模型的多个条目我会使用accepts_nested_attributes_for但是我认为我不需要这个例子吗?

Thanks

2 个解决方案

#1


14  

Ok after some trial and error

经过一些试验和错误后确定

<%= f.collection_select(:sector_id, Sector.all, :id, :name, {:prompt => "Please Select a Sector"}, {:multiple => true}) %>

lets me select multiple options

让我选择多个选项

#2


-1  

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

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

The things that go in the options hash are described at the top of the page, and include :prompt. The html_options hash is for html attributes that you want to set, e.g. multiple, a class, an id.

选项哈希中的内容在页面顶部描述,包括:prompt。 html_options哈希用于您要设置的html属性,例如多个,一个类,一个id。

#1


14  

Ok after some trial and error

经过一些试验和错误后确定

<%= f.collection_select(:sector_id, Sector.all, :id, :name, {:prompt => "Please Select a Sector"}, {:multiple => true}) %>

lets me select multiple options

让我选择多个选项

#2


-1  

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

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

The things that go in the options hash are described at the top of the page, and include :prompt. The html_options hash is for html attributes that you want to set, e.g. multiple, a class, an id.

选项哈希中的内容在页面顶部描述,包括:prompt。 html_options哈希用于您要设置的html属性,例如多个,一个类,一个id。