【问题标题】:How to use collection_select in rails 4 from a model module?如何在模型模块的 rails 4 中使用 collection_select?
【发布时间】:2014-11-09 16:17:57
【问题描述】:

我正在尝试使用关注/模块为默认 _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

这是我要调用部门方法的模型:

    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>
      #...

显然,这不起作用,但我想不出其他任何事情。 我已经在互联网上进行了搜索,但我得到了艰难的解释,而且没有一个涉及模块……这可能吗?

【问题讨论】:

    标签: ruby-on-rails module collection-select


    【解决方案1】:

    解决了!

    我用错了方法..

    我们不能使用带有哈希的 collection_select 帮助器,而是需要使用常规的 select 方法。

    当您有两个模型并且想要在下拉菜单中组合它们的不同值时使用Collection_select。

    有关如何在此处使用带有哈希的 select 标记的信息:

    http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 2017-01-24
      相关资源
      最近更新 更多