私は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