[英]Rails Controller not creating new record
I have a definition 我有一个定义
def create
@person = Person.new(params.require(:firstname, :lastname, :job).permit(loc))
@person.save
redirect_to @person
When I try to save a new person I get the following error: 当我尝试保存新人时,我收到以下错误:
ArgumentError in PeopleController#create
wrong number of arguments (5 for 1)
Is there an issue with the way I coded it? 我编码的方式有问题吗? I also tried separating out each element with a separate ".require".
我也尝试用单独的“.require”分离出每个元素。
Typically you want something like params.require(:person).permit(:firstname, :lastname, :job, :loc)
then have your Person
model validate the required attributes. 通常,您需要类似
params.require(:person).permit(:firstname, :lastname, :job, :loc)
然后让您的Person
模型验证所需的属性。
As for your issue, my guess is if you check your params
you will see something like {person: {firstname: "first", lastname: "last", job: 1, loc: "somewhere"}}
coming through and that is why your Person.new
is raising that error. 至于你的问题,我的猜测是,如果你检查你的
params
你会看到像{person: {firstname: "first", lastname: "last", job: 1, loc: "somewhere"}}
,这是为什么你的Person.new
正在提出这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.