我有一个接受 aninvoice
及其嵌套的模型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
有人可以帮我解决这个问题吗?
谢谢...