How to properly set guides 3 nested attributes

I have 2 models. Member and Poll

member.rb as follows

Class Member < ActiveRecord::Base
  has_one :survey, :dependent => :destroy
  accepts_nested_attributes_for :survey

  attr_accessible :fname,:lname, :address, :city, :state, :zip, :email, :phone, :phone_alt, :e_contact, :e_contact_phone, :physician, :physician_phone, :chiropractor, :chiropractor_phone, :password, :password_confirmation, :remember_me, :survey_attributes

end

survey.rb as follows

Class Survey < ActiveRecord::base
  belongs_to :member
end

however, whenever I try to create an element with polling attributes, I get

ActiveModel :: MassAssignmentSecurity :: Error: Cannot Assign Protected Attributes: Reviews

I am testing this through the console.

+3
source share
1 answer

With an association, an has_oneaccessible call should read:

attr_accessible :survey_attributes

The parameters you submit must be nested, for example:

params = { :member => { :name => 'Jack', :survey_attributes => { :attribute => 'value' } } }

In the form, make sure you embed the nested relationships correctly, i.e. you should use:

= form_for @member do |f|
  ...
  = f.fields_for :survey do |s|
    ...

, . , , , , , .

. # accepts_nested_attributes_for Rails API.

+2

All Articles