Rails 4: accepts_nested_attributes_for and mass assignment

后端 未结 1 1824
北海茫月
北海茫月 2020-12-10 14:23

I am trying to reproduce railscast #196 in Rails 4. However, I\'m experiencing some problems.

In my example I try to generate a Phonebook - each Person could have mu

相关标签:
1条回答
  • 2020-12-10 15:18

    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.

    0 讨论(0)
提交回复
热议问题