ckarbass
ckarbass

Reputation: 3681

Rails 3.2 Can't mass-assign protected attributes: r

I'm using Rails 3.2.X. This is a has_one relationship.

Everything seems buttoned up here:

class P < ActiveRecord::Base
  has_one :r
  accepts_nested_attributes_for :r
  attr_accessible :s, :r_attributes
end

class R < ActiveRecord::Base
  attr_accessible :a, :b, :c
  belongs_to :p
end 

What's posted from the form

"p"=>{"s"=>"5/6/2012", "r"=>{"a"=>"hello", "b"=>"world", "c"=>""}}

Shortened P Form:

<%= form_for(@p) do |f| %>
<%= render :partial => "r/form", :locals => { :p_form => f, :r => @p.r } %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Shortened R Form Partial

<%= p_form.fields_for(r) do |fr| %>

If I create R like this R.new(params[p][r]) it works fine. If I create P like this P.new(params[p]) I get can't mass assign protected attributes for R (even though it works independently) You'll notice I have included r_attributes as accessible.

The correct answer as pointed out below is

<%= promotion_form.fields_for(:r) do |fr| %>

Upvotes: 2

Views: 2190

Answers (1)

Mik
Mik

Reputation: 4187

The form should post r_attributes, not r: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Upvotes: 4

Related Questions