1

我有一个这样定义的模型:

class Order < ActiveRecord::Base
  belongs_to :user

  TYPES = %w[t_01 t_02 t_03]
  validates :order_type, inclusion: { in: TYPES }
end

我正在尝试在视图中创建一个下拉菜单,该菜单将由 TYPES 中可用的值填充。

下面显示的当然不是正确的,因为它使用属于已记录在 DB 中的订单的类型填充下拉菜单:

<div class="field">
  <%= f.label :order_type %><br>
  <%= f.collection_select :order_type, Order.all, :order_type, :order_type %>
</div>

有人可以给我任何提示我该如何解决吗?先感谢您。

4

2 回答 2

2
#model

 def self.types
  TYPES
 end


 #view
 <%= f.collection_select :order_type, Order.types, :to_s, :to_s, {include_blank: false}, {:multiple => false} %>
于 2015-09-07T20:58:32.067 回答
0

您也可以将其用作

<%= f.collection_select :order_type, Order::TYPES , :to_s, :to_s, {include_blank: false}%>

于 2015-09-08T13:21:08.720 回答