0

I'm getting 'Can't mass-assign protected attribute' error when trying to save a form that uses 'accepts_nested_attributes_for'. I think I have code the model correctly but not sure what I missed. Any idea?

error: Can't mass-assign protected attributes: organization

user.rb

class User < ActiveRecord::Base
  belongs_to :organization

  accepts_nested_attributes_for :organization

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :username, :first_name, :last_name, :organization_attributes
end

organization.rb

class Organization < ActiveRecord::Base
  has_many :users

  attr_accessible :address1, :address2, :city, :country, :fax, :name, :phone, :state, :zip
end

users_controller.rb

  def new
    @user = User.new
    @organization = Organization.new

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  def create
    @user = User.new(params[:user])
    @organization = @user.organizations.build(params[:organization])

    respond_to do |format|
      if @user.save
        @organization.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
Azren
  • 227
  • 1
  • 4
  • 16

1 Answers1

0

Manage to get what I want based on the answer on this question: Use both Account and User tables with Devise

By the way I'm also using STI and it works great.

Community
  • 1
  • 1
Azren
  • 227
  • 1
  • 4
  • 16