【发布时间】:2013-08-08 15:38:16
【问题描述】:
好的,所以在安装strong_params 之前,使用MyBook.create! 可以,但现在不行了。
这是我的代码
class AuthorsController < ApplicationController
def new
@author = user.authors.build
end
def create
@author = Author.new(author_params)
if @author.save
@book = MyBook.create!(:author_id => @author.id,
:user_id => @author.user_id)
)
else
render :new
end
end
我试过了
@book = MyBook.create!(params.require(:my_book).permit(
:author_id => @author.id,
:user_id => @author.user_id)
)
但我收到Required parameter missing: my_book
我做错了什么?我可以一个一个地更新每个属性,但这似乎效率不高。我知道我不能批量分配受保护的属性,但不必在我的模型中分配属性(因为 strong_params),我不明白如何让它工作。
提前谢谢你
【问题讨论】:
-
你的创建不起作用,因为你没有先
.save你的@author对象,所以它没有id。使用@author = Author.create(author_params)而不是新的 -
@MrYoshiji 抱歉,我实际上是在保存作者并忘记将其包含在其中。我刚刚编辑了它
标签: ruby-on-rails strong-parameters