【问题标题】:Create checkbox array in form_for rails在 form_for rails 中创建复选框数组
【发布时间】:2013-04-03 06:26:47
【问题描述】:

我有这个 _form 文件。

<%= form_for(@company) do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.label :company_type %>
  <%= f.select :company_type_id, CompanyType.order(:name).map{|x| [x.name, x.id]} %>        
  <%= f.label :description %>
  <%= f.text_area :description %>
  <%= f.submit %>
<% end %>

我的问题是如何更改选择

<%= f.select :company_type_id, CompanyType.order(:name).map{|x| [x.name, x.id]} %>  

以这种形式进入复选框列表?

公司类型模型:

class CompanyType < ActiveRecord::Base
  attr_accessible :name
  has_many :companies
end

公司模式:

class Company < ActiveRecord::Base
  attr_accessible :description, :name, :company_type_id, :website
  belongs_to :type, :class_name => "CompanyType", :foreign_key => :company_type_id
end

【问题讨论】:

    标签: ruby-on-rails arrays select checkbox form-for


    【解决方案1】:

    试试这个

    <%= f.check_box :company_type_id, CompanyType.order(:name).map{|x| [x.name, x.id]} %>
    

    【讨论】:

    • 对不起,我只是编辑答案,解决方案由@Ganesh 提出;)
    • @tkanzakic 它仍然不起作用。它说“[["Oil", 3], ["Finance", 2], ["IT", 1]]:Array"的未定义方法`合并'
    【解决方案2】:
    猜你喜欢
    • 2018-06-22
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多