简体   繁体   English

ruby on rails如何使用FormOptionHelpers创建动态下拉列表

[英]ruby on rails how to use FormOptionHelpers to create dynamic drop down list

I have checked some tutorials but I got confused by the parameters in this method 我检查了一些教程,但是我对这种方法中的参数感到困惑

collection_select (object, attribute, collection, value_method, text_method, options = {}, html_options ={}) collection_select(对象,属性,集合,value_method,text_method,options = {},html_options = {})

I have a map model includes: :area, :system, :file 我有一个地图模型,包括::area,:system,:file

and I want to read :area from database to a drop down list, and let user choose one 我想从数据库中读取:area到一个下拉列表,然后让用户选择一个

I already did @map = Map.all in the view 我已经在视图中执行了@map = Map.all

what the method should be? 方法应该是什么?

especially the parameter "attribute". 特别是参数“属性”。 In a lot tutorials, people put "id" here. 在很多教程中,人们在此处放置“ id”。 But I don't know what "id" is, and in my situation I don't need any other value, just the "area". 但是我不知道“ id”是什么,在我的情况下,我不需要任何其他值,只需要“ area”。

Im not exactly sure what you are asking here but if you are trying to make a dropdown selection for use in an html form will this example help you at all? 我不能完全确定您在这里要问什么,但是如果您尝试选择要用于html表单的下拉列表,此示例将完全对您有所帮助吗?

<% nations = {'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico' => 'Mexico', 'United Kingdom'=> 'UK'} %>
<% list = nations.sort %>
<%= f.select :country, list, %>

Here nations is a hash of countries then list becomes the sorted copy of that hash. 这里的国家是国家的哈希,然后列表成为该哈希的排序副本。 An html dropdown is then created as a part of the form "f". 然后,将HTML下拉列表创建为“ f”形式的一部分。 ":country" is the part of the model that the data is connected to while list is the options to populate the dropdown with “:country”是数据连接到的模型的一部分,而list是用于填充下拉列表的选项

It's not clear from your question what the model is that's being populated with the area. 从您的问题尚不清楚该区域填充了什么模型。

Typically, collection_select is used between related models. 通常,在相关模型之间使用collection_select。

eg. 例如。

class Category < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :category
end

When selecting the 'category' for a product, your view would have something like: 为产品选择“类别”时,您的视图将具有以下内容:

<%= f.collection_select(:category_id, :id, Category.all, :name, include_blank: true) %>

What this does is specify the Product.category_id as the attribute being populated with the value of Category.id. 这是将Product.category_id指定为要使用Category.id值填充的属性。 The values come from the Category.all collection, and with Category.name being the item displayed in the select. 这些值来自Category.all集合,其中Category.name是select中显示的项目。 The last (optional) parameter says to include a blank entry. 最后一个(可选)参数说要包含一个空白条目。

Something like the following is probably what you need: 您可能需要以下内容:

<%= f.collection_select(:map_id, :id, @map, :area) %>

However, if the model you're trying to populate has an area attribute (instead of an ID linking to the map), you might need to use: 但是,如果要填充的模型具有area属性(而不是链接到地图的ID),则可能需要使用:

<%= f.collection_select(:area, :area, @map, :area) %>

This specifies that the area attribute of the receiving table will be populated with Map's area attribute, which is also being used as the "description" in the select. 这指定将使用Map的area属性填充接收表的area属性,该属性也将用作select中的“描述”。

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

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