rails 具有一维数据的模型 select 框的形式
rails form for model select box with 1-dimensional data
我想将模型中文本字段的输入可能性限制为先前定义的数组。
如何只用一个像 ["foo","bar","foobar"]
这样的一维数组来创建 options_for_select
?
我试过了
form_for @mappings do |f|
f.select(:mapping_type, options_for_select(["foo","bar","foobar"]), class: "..."
end
但是 select 盒子出来的时候一团糟:
<select name="section_mapping[mapping_type]" id="section_mapping_mapping_type">
与其应有的相反:
<select name="mapping_type" >
编辑:
我将 f.select
更改为 select_tag
并且表单显示没有任何错误,但是当我提交它时,它将该字段留空
编辑 2:
f.collection_select(:mapping_type, options_for_select([...]), class: "..."
的工作原理是正确提交具有值的表单,但未应用 HTML class。这是为什么?
基本上,您希望能够将 collection select 绑定到 object 的 属性(在您的情况下,@mappings
)
此外,根据 rails collection_select 上的文档,它将采用以下选项:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) public
- 对象: object 在这种情况下,您将 selected 选项绑定到 (@mappings [
f
])
- 方法:object的property/attribute(本例为
mapping_type
)
- collection: collection for select (
["foo","bar","foobar"]
)
- value_method: 您希望随提交一起发回的值(请注意,这是一个
method
,这意味着您应该能够调用它在 object.) 稍后详细介绍。
- text_method: 你想在视图的select选项上显示为文本的值(这也是上面的方法,更多稍后也会讨论)
- 选项:您想要的任何附加选项,(例如:
include_blank
)
- html_options: 例如:
id
、class
等
关于 value_method
和 text_method
,这些是应该在您的 collection
上调用的方法,这意味着您的 collection 将是 objects.
为此,您可以有以下几点:
class CollectionArr
include ActiveModel::Model
attr_accessor :name
ARR = [
{"name" => "foo"},
{"name" => "bar"},
{"name" => "foobar"}
]
def self.get_collection
ARR.collect do |hash|
self.new(
name: hash['name']
)
end
end
end
从这里开始,调用 CollectionArr.get_collection
将 return 一个 object 的数组,您可以在其中调用 .name
到 return foo
、bar
或 foobar
。这使得使用 collection_select
和从这里轻松交易:
<%= f.collection_select : mapping_type, CollectionArr.get_collection, :name, :name, {:include_blank => "Select one"} %>
一切都是绿色的...
我想将模型中文本字段的输入可能性限制为先前定义的数组。
如何只用一个像 ["foo","bar","foobar"]
这样的一维数组来创建 options_for_select
?
我试过了
form_for @mappings do |f|
f.select(:mapping_type, options_for_select(["foo","bar","foobar"]), class: "..."
end
但是 select 盒子出来的时候一团糟:
<select name="section_mapping[mapping_type]" id="section_mapping_mapping_type">
与其应有的相反:
<select name="mapping_type" >
编辑:
我将 f.select
更改为 select_tag
并且表单显示没有任何错误,但是当我提交它时,它将该字段留空
编辑 2:
f.collection_select(:mapping_type, options_for_select([...]), class: "..."
的工作原理是正确提交具有值的表单,但未应用 HTML class。这是为什么?
基本上,您希望能够将 collection select 绑定到 object 的 属性(在您的情况下,@mappings
)
此外,根据 rails collection_select 上的文档,它将采用以下选项:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) public
- 对象: object 在这种情况下,您将 selected 选项绑定到 (@mappings [
f
]) - 方法:object的property/attribute(本例为
mapping_type
) - collection: collection for select (
["foo","bar","foobar"]
) - value_method: 您希望随提交一起发回的值(请注意,这是一个
method
,这意味着您应该能够调用它在 object.) 稍后详细介绍。 - text_method: 你想在视图的select选项上显示为文本的值(这也是上面的方法,更多稍后也会讨论)
- 选项:您想要的任何附加选项,(例如:
include_blank
) - html_options: 例如:
id
、class
等
关于 value_method
和 text_method
,这些是应该在您的 collection
上调用的方法,这意味着您的 collection 将是 objects.
为此,您可以有以下几点:
class CollectionArr
include ActiveModel::Model
attr_accessor :name
ARR = [
{"name" => "foo"},
{"name" => "bar"},
{"name" => "foobar"}
]
def self.get_collection
ARR.collect do |hash|
self.new(
name: hash['name']
)
end
end
end
从这里开始,调用 CollectionArr.get_collection
将 return 一个 object 的数组,您可以在其中调用 .name
到 return foo
、bar
或 foobar
。这使得使用 collection_select
和从这里轻松交易:
<%= f.collection_select : mapping_type, CollectionArr.get_collection, :name, :name, {:include_blank => "Select one"} %>
一切都是绿色的...