0

我正在使用 simple_forms,我的目标是根据选择创建一个复选框。我应该将它保存在关联表中。

我写的模型像

class Purchase < ActiveRecord::Base

  attr_accessible :UserName, :cardno, :ctype
  has_many :items
end

class CreateItems < ActiveRecord::Migration
  def change
    create_table :items do |t|
      t.timestamps
    end
  end
end

class AddField < ActiveRecord::Migration
  def up
    add_column :purchases, :ctype, :string
    add_column :items, :item_name, :string
     add_column :items, :purchase_id, :integer
  end

  def down
    
    remove_column :purchases, :ctype
    remove_column :items, :item_name
     remove_column :items, :purchase_id
  end
end

我写了 View like

<%= simple_form_for @purchase, :url => { :controller => 'payments', :action => 'show' } do |f| %>

<%= f.input :UserName, :label => "UserName:", :wrapper_html => { :class => 'uname_div'} %>
<%= f.input :cardno, :label => "Number On the Card:", :wrapper_html => { :class => 'uname_div'} %>
 <%= f.collection_radio_buttons :ctype, [[true, 'DebitCard'], [false, 'CreditCard']], :first, :last %>
<%= f.association :items, :label =>" Items Purchased:", :collection => ["Ac","Mobile","fridge"], :as => :check_boxes %>
<%= f.button :submit, :value => "Submit", :class => "sub_div" %>
<% end %>

当我提交数据时,它会给出一个错误,例如

ActiveModel::MassAssignmentSecurity::PaymentsController#show 中的错误

无法批量分配受保护的属性:item_ids

4

1 回答 1

0

您必须添加item_ids到模型中的列表中,如下所示:

attr_accessible :UserName, :cardno, :ctype, :item_ids

这会将给定属性列入白名单并允许进行批量分配。

于 2013-03-07T05:08:52.883 回答