导轨形式用于具有一维数据的模型选择框 [英] rails form for model select box with 1-dimensional data

查看:149
本文介绍了导轨形式用于具有一维数据的模型选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将我的模型中的文本字段的输入可能性限制为先前定义的数组。我如何使 options_for_select 只需要像 [foo,bar,foobar]一维数组



我试过

  form_for @mappings do | f | 
f.select(:mapping_type,options_for_select([foo,bar,foobar]),class:...

end

但是选择框出来都是这样的:

 < 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类没有被应用为什么是这样?

解决方案

基本上,你想要把你的coll选择对象的属性(在您的情况下, @mappings



另外,从文档 rails collection_select ,它将采取如下选项:


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





  • Objet:将您选择的选项绑定到的对象(@mappings [ f ])在这种情况下

  • 方法:对象的属性/属性(在这种情况下, mapping_type

  • 集合:select( [foo,bar,foobar

  • value_method:要使用submit发回的值(请注意,这是一个方法这意味着你应该能够校准

  • text_method:要在视图上的select选项上显示为文本的值(这是还有一个方法如上所述,更多的还有)

  • 选项:你想要的任何其他选项,(例如: include_blank

  • html_options:例如: id 等。



关于 value_method text_method ,这些是在集合上调用的方法,这意味着您的集合将是一个



为此,您可以进行以下操作:

  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 将返回一个对象数组, code> .name 返回 foo bar foobar的。这使得使用 collection_select 并从这里轻松处理:

  %= f.collection_select:mapping_type,CollectionArr.get_collection,:name,:name,{:include_blank => 选择一个}%> 

所有都是绿色...


I want to limit the entry possibilities for a text field in my model to a previously defined array.

How do I make an options_for_select with just a 1-dimensional array like ["foo","bar","foobar"]?

I tried

form_for @mappings do |f|
  f.select(:mapping_type, options_for_select(["foo","bar","foobar"]), class: "..."

end

But the select box comes out all messed up like this:

<select name="section_mapping[mapping_type]" id="section_mapping_mapping_type">

as opposed to what it should be:

<select name="mapping_type" >

EDIT:

I changed the f.select to select_tag and the form shows up without any errors but when I submit it, it leaves that field empty

EDIT 2:

f.collection_select(:mapping_type, options_for_select([...]), class: "..."

works as in it submits the form with the value correctly, but the HTML class is not applied. Why is that?

解决方案

Basically, you want to be able to tie your collection select to a property of the object (in your case, @mappings)

Also, from the doc on rails collection_select, it will take options as follow:

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

  • Objet: the object you are binding the selected option to (@mappings [f]) in this case
  • method: The property/attribute of the object (in this case, mapping_type)
  • collection: The collection for select ( ["foo","bar","foobar"] )
  • value_method: The value you want to send back with the submit (Note that this is a method which means you should be able to call it on an object.) more on this later.
  • text_method: The value you want to show as text on the select option on the view (this is also a method as above, more on this later as well)
  • options: any additional option you want, (e.g: include_blank)
  • html_options: eg: id, class etc.

As concerning the value_method and text_method, these are methods that should be called on your collection, which means that your collection will be an array of objects.

To this end, you can have the following:

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

From here, calling CollectionArr.get_collection will return an array of objects where you can cal .name to return either foo, bar, or foobar. This makes using the collection_select and easy deal from here:

<%= f.collection_select : mapping_type, CollectionArr.get_collection, :name, :name, {:include_blank => "Select one"} %>

And all is green...

这篇关于导轨形式用于具有一维数据的模型选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆