Ruby on Rails:下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1956191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: Drop down menu
提问by kikito
I'm trying to create a drop down menu to allow a user to change an entry's field in my table. The user has one of three options -- hot, medium and cold.
我正在尝试创建一个下拉菜单,以允许用户更改我表中的条目字段。用户可以选择三个选项之一——热、中和冷。
I already have text_fieldsthat do essentially the same thing for other fields, that all update when the user clicks on a submit_tag.
我已经text_fields对其他字段做了基本相同的事情,当用户单击submit_tag.
Is there an easy way to implement a drop-down box and have the result saved with the submit_tag?
有没有一种简单的方法来实现下拉框并将结果与submit_tag?
thanks,
谢谢,
-Chris
-克里斯
回答by Mark Swardstrom
Here's the basic answer. The array of two element arrays is the critical part.
这是基本答案。两个元素数组的数组是关键部分。
<% form_for @entry do |f| %>
<%= f.text_field :name %>
<%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
<%= f.submit %>
<% end %>
回答by kikito
I'll assume 2 things:
我会假设两件事:
- That you are the
<%= form_for @model_instanceidiom (explained on section 2.2 of this guide). - That you want to store the "hot", "medium" and "cold" values as strings (not as numbers 1,2 and 3 or something similar) on your database.
- 你就是
<%= form_for @model_instance惯用语(在本指南的第 2.2 节中有解释)。 - 您想将“热”、“中”和“冷”值作为字符串(而不是数字 1,2 和 3 或类似的数字)存储在数据库中。
Let's say that you have two fields, called :nameand :temperature, controlled by two text_fields:
假设您有两个字段,称为:nameand :temperature,由两个控制text_fields:
<% form_for @article do |f| %>
<%= f.text_field :name %>
<%= f.text_field :temperature %>
<%= f.submit "Create" %> <% end %>
<% end %>
Now you want to change the :temperature control to a dropdown list, accepting hot, medium and cold as values. Then you can do that this way:
现在您想将 :温度控制更改为下拉列表,接受热、中和冷作为值。然后你可以这样做:
<% form_for @article do |f| %>
<%= f.text_field :name %>
<%= f.collection_select :temperature, Article::TEMPERATURES, :to_s, :to_s,
:include_blank => true
%>
<%= f.submit "Create" %> <% end %>
<% end %>
You will now have to define the Article::TEMPERATURESconstant in your Article model. It shouldn't be very difficult:
您现在必须Article::TEMPERATURES在您的文章模型中定义常量。这应该不是很困难:
class Article < Activerecord::Base
TEMPERATURES = ['hot', 'medium', 'cold']
You may be wondering why I added the :include_blankpart on the collection_select. This will add an "empty" option on your dropdown list. You will need that empty option when creating new objects, unless you want a "default" value to temperature.
您可能想知道为什么我:include_blank在collection_select. 这将在您的下拉列表中添加一个“空”选项。创建新对象时将需要该空选项,除非您想要温度的“默认”值。
回答by Ben
回答by mewallacee
I was working on something similar. I got this to work by simply adding either an enum or a constant(similar to what kikito said previously) in my model and then calling the select in my form.
我正在做类似的事情。我通过简单地在我的模型中添加一个枚举或一个常量(类似于 kikito 之前所说的)然后在我的表单中调用 select 来实现这一点。
Here's how it can work.
这是它的工作原理。
using the constant:
使用常量:
class ClassName < ActiveRecord::Base TEMPERATURES = ['Hot', 'Medium', 'Cold'] end
class ClassName < ActiveRecord::Base TEMPERATURES = ['Hot', 'Medium', 'Cold'] 结束
bin/rails g migration add_column_to_table temperatures:string
bin/rails g 迁移 add_column_to_table 温度:字符串
_form.html.erb
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName::TEMPERATURE %>
or
或者
using the enum:
使用枚举:
class ClassName < ActiveRecord::Base enum temperature: [:hot, :medium, :cold] end
class ClassName < ActiveRecord::Base enum temperature: [:hot, :medium, :cold] 结束
bin/rails g migration add_column_to_table temperatures:integer
bin/rails g 迁移 add_column_to_table 温度:整数
_form.html.erb
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName.temperatures.keys %>
Hope that helps you!
希望能帮到你!
回答by Chandra Patni
You might want to consider formtasticgem which is lot less code.
您可能想考虑使用更少代码的formtasticgem。
<% semantic_form_for @stuff do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :temperature, :as => :select,
:label => "Degree", :include_blank => false,
:collection => [["Hot", 1], ["Medium", 2], ["Cold", 3]] %>
<% end %>
<%= f.buttons %>
<% end %>
回答by ekushay
In accordance with all of the above answers, remember to do this last, important step:
根据以上所有答案,请记住执行最后的重要步骤:
Restart your server!
重启你的服务器!
As a newbie, I was wondering why my array was not working even though I followed all the steps correctly.
作为新手,我想知道为什么即使我正确地遵循了所有步骤,我的阵列仍然无法正常工作。

