invoice
とそのネストされた を受け入れるモデルがありますitems
:
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items
attr_accessible :number, :date, :recipient, :project_id, :items_attributes
accepts_nested_attributes_for :items, :reject_if => :all_blank
end
ただし、これを RSpec と FactoryGirl でテストするのは非常に難しいと思います。これは私が持っているものです:
describe 'POST #create' do
context "with valid attributes" do
it "saves the new invoice in the database" do
expect {
post :create, invoice: attributes_for(:invoice), items_attributes: [ attributes_for(:item), attributes_for(:item) ]
}.to change(Invoice, :count).by(1)
end
end
end
これは、コントローラーでの私の作成アクションです。
def create
@invoice = current_user.invoices.build(params[:invoice])
if @invoice.save
flash[:success] = "Invoice created."
redirect_to invoices_path
else
render :new
end
end
これを実行するたびに、エラーが発生します。Can't mass-assign protected attributes: items
誰でもこれについて私を助けることができますか?
ありがとう...