ruby on rails - Can't mass-assign protected attributes:: issue while building a nested attribute form -


i've spent day on particular issue , although there other posts it, i've not resolved particular issue.

i've tried following railscast #196 still can't identify mistake.

models:

exercises

# == schema information # # table name: exercises # #  id          :integer          not null, primary key #  name        :string(255) #  description :text #  created_at  :datetime         not null #  updated_at  :datetime         not null #  image       :string(255) #  class exercise < activerecord::base   attr_accessible :description, :name, :tags_attributes   has_many :tags   has_one :difficulty   accepts_nested_attributes_for :tags, :allow_destroy => true end 

tags

# == schema information # # table name: tags # #  id         :integer          not null, primary key #  name       :string(255) #  created_at :datetime         not null #  updated_at :datetime         not null #  class tag < activerecord::base   attr_accessible :name, :exercise_id   belongs_to :exercise   accepts_nested_attributes_for :exercises end 

form

<%= form_for(@exercise) |f| %>   <% if @exercise.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@exercise.errors.count, "error") %> prohibited exercise being saved:</h2>        <ul>       <% @exercise.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>    <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :description %><br />     <%= f.text_area :description %>   </div>   <%= f.fields_for :tag |builder| %>     <div class="field">         <%= builder.label :name, "tags" %><br />         <%= builder.text_field :name %>          </div> <% end %>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

the error, specifically, when submit form is:

activemodel::massassignmentsecurity::error in exercisescontroller#create  can't mass-assign protected attributes: tag  application trace | framework trace | full trace app/controllers/exercises_controller.rb:42:in `new' app/controllers/exercises_controller.rb:42:in `create' 

in form, replace line

<%= f.fields_for :tag |builder| %> 

with

<%= f.fields_for :tags |builder| %> 

in model you're using attr_accessible , add plural followed _attributes can set attributes, in form called singular tag hence why you're getting mass-assign protected attributes error.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -