[英]Ruby on Rails: Drop down menu
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_fields
that 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 -克里斯
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 %>
I'll assume 2 things: 我假设有两件事:
<%= form_for @model_instance
idiom (explained on section 2.2 of this guide ). <%= form_for @model_instance
成语(在本指南的第2.2节中解释)。 Let's say that you have two fields, called :name
and :temperature
, controlled by two text_fields
: 假设您有两个字段,
:name
为:name
和: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::TEMPERATURES
constant in your Article model. 您现在必须在文章模型中定义
Article::TEMPERATURES
常量。 It shouldn't be very difficult: 这应该不是很困难:
class Article < Activerecord::Base
TEMPERATURES = ['hot', 'medium', 'cold']
You may be wondering why I added the :include_blank
part on the collection_select
. 您可能想知道为什么我在
collection_select
上添加了:include_blank
部分。 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.
在创建新对象时,您将需要该空选项,除非您想要一个“默认”值到温度。
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 migration add_column_to_table temperature:string
_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枚举温度:[:hot,:medium,:cold] end
bin/rails g migration add_column_to_table temperatures:integer bin / rails g migration add_column_to_table temperature:integer
_form.html.erb _form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName.temperatures.keys %>
Hope that helps you! 希望对你有所帮助!
You might want to consider formtastic gem which is lot less code. 您可能想要考虑更少代码的formtastic gem。
<% 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 %>
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. 作为一个新手,我想知道为什么我的阵列不工作,即使我正确地遵循了所有步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.