[英]How to use collection_select in rails 4 from a model module?
我正在嘗試使用關注/模塊為默認的_form.html.erb使用collection_select標記,我需要設置一個包括一些部門名稱的哈希值。
這是我的app / models / concerns / SetDepartment.rb
module Set_Department
extend ActiveSupport :: Concern
def department
department {
1=>"Amatitlán",
2=>"Chinautla",
3=>"Chuarrancho"
}
end
end
這是我要調用Department方法的模型:
class Aplicante < ActiveRecord::Base
include SetDepartment
validates :titulo_id, :primer_nombre,
:primer_apellido, :dpi, :direccion_linea_1,:zona, :department_id, :username,
presence: true
validates :dpi,:username, uniqueness: true
has_secure_password
end
現在,我需要在我的app / views / applicants / _form.html.erb上的collection_select標記中包含此哈希
#...
<div class="field">
<%= f.label :department_id %><br>
<%= f.collection_select :department_id, Aplicante.department, Aplicante.department %>
</div>
#...
顯然,這是行不通的,但是我不能考慮其他任何事情。 我已經在互聯網上進行搜索,但是我得到的只是艱難的解釋,而且沒有一個涉及模塊……甚至可能嗎?
解決了!
我使用了錯誤的方法。
我們不能將collection_select幫助器與哈希一起使用,相反,我們需要使用常規的select方法。
當您有兩個模型並且要在下拉菜單中組合它們的不同值時,將使用Collection_select。
有關如何將select標記與哈希一起使用的信息,請參見:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.