4

I've seen other questions with this problem, but so far the answers haven't worked for me. I'm trying to have a form that registers a User, and creates an Organization at the same time. The User and Organization are associated via an assignment table.

Here is my form:

= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|

  = devise_error_messages!

  = f.fields_for :organizations do |f|

    = f.label :name
    = f.text_field :name

  = f.label :email
  = f.email_field :email

  = f.label :password
  = f.password_field :password

  = f.label :password_confirmation
  = f.password_field :password_confirmation

My Registration controller:

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    @user = User.new
    @user.organizations.build
  end

  def create
    super
  end

  def update
    super
  end
end

My Organization model:

class Organization < ActiveRecord::Base
  has_many :organization_assignments
  has_many :users, :through => :organization_assignments

  attr_accessible :name
end

and my User model:

class User < ActiveRecord::Base

  has_many :organization_assignments
  has_many :organizations, :through => :organization_assignments

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  accepts_nested_attributes_for :organizations

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :organization_attributes
  # attr_accessible :title, :body

end

The exact error I'm getting is:

Can't mass-assign protected attributes: organizations_attributes

Asherlc
  • 1,111
  • 2
  • 12
  • 28

1 Answers1

9

You have to add :organizations_attributes to attr_accessible in the User model.

deefour
  • 34,974
  • 7
  • 97
  • 90
  • Is the difference between that and what he already had in the User model the plural :organizations_attributes instead of the singular :organization_attributes? I get the same error message, but this did not fix it. – Mittenchops Aug 10 '12 at 19:31
  • 2
    He had `accepts_nested_attributes_for :organizations` in his `User` model. Take that symbol and append `_attributes` and you get `:organizations_attributes` – deefour Aug 10 '12 at 19:34
  • @Mittenchops Did you seriously downvote my answer? My answer solves the OP's question; it's not intended to solve yours (since I've never seen it). That is *not* a reason to downvote my answer. – deefour Aug 10 '12 at 19:35
  • He had `accepts_nested_attributes_for :organizations` and `attr_accessible :email ... :organization_attributes` You're saying it should be `accepts_nested_attributes_for :organizations_attributes` instead? – Mittenchops Aug 10 '12 at 19:45
  • @Deefour, the answer was (to me) kind of confusing and less clear than it could be. Maybe the OP revised his question to include the `attr_accessible ... :organization_attributes` after you answered, I don't know. But it's unclear to me exactly how your answer fits into the above, which is why I asked a question. – Mittenchops Aug 10 '12 at 19:55
  • 1
    I'm happy to answer you're questions, however your lack of comprehension of OP's question and my answer is not reason to downvote me. From **[the down-vote privilege page](http://stackoverflow.com/privileges/vote-down)**: "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect.". My answer is the opposite of this; correct, clear, and concise. – deefour Aug 10 '12 at 20:13
  • 1
    I see what you're saying, but disagree with you and spent my meager rep points voting that way. I found it a single line 'no-effort-expended post.' If you were to clarify even very little relating your answer to his question (as in, "replace line `A` with `B`") showing where it fits in clearly and concisely, it would be a substantially more complete answer. Your answer says the solution to his problem is "attr_accessible :email, :password, :password_confirmation, :remember_me, :organization_attributes *:organizations_attributes*: with the last one doubled and pluralized. Is that true? – Mittenchops Aug 10 '12 at 20:27
  • 1
    Considering that's exactly what I did *(told OP what to change and where)*, I give up; **I can live without 2 points**. Good luck solving your problem. – deefour Aug 10 '12 at 20:33
  • Thanks, I needed that ! I followed a RailsCast (#196) about nested attributes and this point is not specified in the tutorial. – Gaetan Jul 27 '13 at 21:19
  • Could anyone explain why this works? So to be more clearly: why is the suffix "_attributes" added to the attribute organizations? Is there any documentation about adding "_attributes" to rails models for active admin in combination with has many relation? – Murmel Dec 19 '13 at 10:41
  • When you define `accepts_nested_attributes_for :organization` on the `User` model, Rails creates a writer method `organization_attributes=`. It's this methods you need to white-list with `attr_accessible` in order for Rails to use it with the mass assignment. **[Here is where the writer is created](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L337-L347)** – deefour Dec 19 '13 at 16:54