[英]How to use collection_select in rails 4 from a model module?
I am trying to use collection_select tag for the default _form.html.erb using a concern/module, I need to set a hash including some department names. 我正在尝试使用关注/模块为默认的_form.html.erb使用collection_select标记,我需要设置一个包括一些部门名称的哈希值。
Here is my app/models/concerns/SetDepartment.rb 这是我的app / models / concerns / SetDepartment.rb
module Set_Department
extend ActiveSupport :: Concern
def department
department {
1=>"Amatitlán",
2=>"Chinautla",
3=>"Chuarrancho"
}
end
end
Here is the model where I want to call the department method: 这是我要调用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
Now, I need to include this hash in a collection_select tag on my app/views/applicants/_form.html.erb 现在,我需要在我的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>
#...
Obviously, this does not work but I can not think on anything else. 显然,这是行不通的,但是我不能考虑其他任何事情。 I have searched through the internet but I just get tough explinations and none of them involves a module... is it even possible? 我已经在互联网上进行搜索,但是我得到的只是艰难的解释,而且没有一个涉及模块……甚至可能吗?
Solved! 解决了!
I was using the wrong method.. 我使用了错误的方法。
We can not use a collection_select helper with a hash, instead, we need to use the regular select method. 我们不能将collection_select帮助器与哈希一起使用,相反,我们需要使用常规的select方法。
Collection_select is used when you have two models and you want to combine their different values in a drop down menu. 当您有两个模型并且要在下拉菜单中组合它们的不同值时,将使用Collection_select。
Information about how to use the select tag with a hash here: 有关如何将select标记与哈希一起使用的信息,请参见:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.