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のように書いています

<%= 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 に答える