簡體   English   中英

Rails-collection_select-用模型中列出的值填充

[英]Rails - collection_select - populate with the values listed in a model

我有一個這樣定義的模型:

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>

有人可以給我任何提示我如何解決的嗎? 先感謝您。

#model

 def self.types
  TYPES
 end


 #view
 <%= f.collection_select :order_type, Order.types, :to_s, :to_s, {include_blank: false}, {:multiple => false} %>

您也可以將其用作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM