Rails 4: accepts_nested_attributes_for and mass assignment

空扰寡人 提交于 2019-11-28 11:07:42

In order to get three phones in the view change form_for :person to form_for @person (you want to use the object you've built here) as follows:

<%= form_for @person, url: people_path do |f| %>

This should fix the ForbiddenAttributes error as well.

And your create action could be:

def create
    @person = Person.create(person_params)

    redirect_to people_path
end

Update:

<%= form_for :person do |f| %> creates a generic form for the Person model and is not aware of the additional details you apply to a specific object (in this case @person in your new action). You've attached three phones to the @person object, and @person is not the same as :person which is why you didn't see three phone fields in your view. Please reference: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for for further details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!