2

I spent so much time on this and lost half my hair by pulling it. Help.

user:

    has_may :rights
    attr_accessible :rights_attributes

right:

    belongs_to :user
    attr_accessible :user_id, :pgd_id, :link_id

View:

<%= semantic_form_for @user, :url => {:action => "rightsupdate", :id => @user.id} do |f| %>$
  <%= f.inputs do %>$
    <%= f.input :pgds, :as => :check_boxes, :required => false %>$
  <% end %>$
    <%= f.input :link_id, :value => @owner.link_id, :as => :hidden %>$
    <%= f.buttons %>$
<% end %>$

but when I update it keeps saying:

WARNING: Can't mass-assign protected attributes: pgd_ids, link_id

I can bypass the pdg by adding :pgd_ids to the user attr_accessible but not the link_id. The link_id is inserted as NULL.

nafkot
  • 103
  • 1
  • 7

1 Answers1

0

First you need to make sure that the model includes accepts_nested_attributes.

user.rb

attr_accessible :rights_attributes
has_may :rights
accepts_nested_attributes :rights

You then need to wrap the rights fields into a fields_for block. It looks like you are using Formtastic, which I'm not familiar with but I think it should look something like this:

<%= semantic_form_for @user, :url => {:action => "rightsupdate", :id => @user.id} do |f| %> 
  <%= f.semantic_fields_for :rights do |builder| %>
    <%= builder.inputs do %>
      <%= builder.input :pgds, :as => :check_boxes, :required => false %>
    <% end %>
    <%= builder.input :link_id, :value => @owner.link_id, :as => :hidden %>
    <%= builder.buttons %>
  <% end %>
<% end %>

Railscasts have a number of free screencasts on nested forms which would be worth checking out the above code doesn't solve the problem.

nmott
  • 9,454
  • 3
  • 45
  • 34
  • Thanks. accepts_nested_attributes_for :rights is already there. your view code ended up duplicating the options. I'll try modifying it. – nafkot Feb 17 '12 at 01:40
  • The above solution does not update. but thanks for the response @nmott my old one still says can't mass update – nafkot Feb 17 '12 at 02:01