私はnested_formジェムを使用しました。フォームに何かを送信しようとすると、モデルCan't mass-assign protected attributes:items
を既に入れていても、メッセージが表示attr_accessible
されます。
形:
<%= nested_form_for(@goods_in) do |f| %>
...
<%= f.fields_for :items do |i| %>
<td><%= i.text_field :description, :autocomplete => :off%></td>
<td><%= i.text_field :quantity, :autocomplete => :off %></td>
<th><%= i.link_to_remove "Remove this item" %></th>
<% end %>
<%= f.submit :"Submit Delivery" %>
<% end %>
Goods In Model:class GoodsIn < ActiveRecord::Base belongs_to :supplier has_many :items
attr_accessible :c4lpono,
:courier,
:deliverydate,
:deliverynoteno,
:destination,
:notes,
:quantity,
:signedby,
:supplier_id,
:partcode_ids
accepts_nested_attributes_for :supplier
validates :c4lpono,
:deliverydate,
:deliverynoteno,
:destination,
:quantity,
:signedby,
:presence =>true
end
項目モデル
class Item < ActiveRecord::Base
belongs_to :goods_in
attr_accessible :quantity,
:partcode,
:description,
:goods_in_id
accepts_nested_attributes_for :goods_in
end
コントローラーの商品:
def create
@goods_in = GoodsIn.new(params[:goods_in])
end