繁体   English   中英

如何在模型模块的rails 4中使用collection_select?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM