2
votes

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.

1
sorry the code outline got messed up. It was working fine when I added it - nafkot
I fiddled with it a little bit -- you need blank lines to separate paragraphs from code formatting. - sarnold
you have a typo in your user model. it should be: has_many :rights - NARKOZ

1 Answers

0
votes

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.