Cannot assign protected attributes in bulk: profiles,

I read a lot of related posts, but can't find why it doesn't work for me. I still have β€œI can’t assign protected attributes: profiles” ... What am I doing wrong?

I have a user model and a related one-to-one profile model. Here's the user model (simplified)

class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation, :profile_attributes, :profile_id has_secure_password has_one :profile accepts_nested_attributes_for :profile end 

Profile model

 class Profile < ActiveRecord::Base attr_accessible :bio, :dob, :firstname, :gender, :lastname, :user_id belongs_to :user end 

my user controller

 def new @user = User.new respond_to do |format| format.html # new.html.erb format.json { render json: @user } end end def create @user = User.new(params[:user]) @user.build_profile respond_to do |format| if @user.save format.html { redirect_to @user, flash: {success: 'User was successfully created. Welcome !'} } format.json { render json: @user, status: :created, location: @user } else format.html { render action: "new" } format.json { render json: @user.errors, status: :unprocessable_entity } end end end 

If this can help, both users and the profile have been tinted.

I also tried using ': profiles_attributes' instead of 'profile_attributes' in User attr_accessible, same problem ...

also tried "@ user.profiles.build" instead of "@ user.build_profile" in the user controller ... same result ...

Any help with some explanation would be great (I'm noob on the rails, so forgive me)

EDIT The simple form I use

 <%= simple_form_for(@user) do |f| %> <%= f.error_notification %> <%= f.simple_fields_for :profiles do |p| %> <div class="nested-form-inputs"> <%= p.input :lastname %> <%= p.input :firstname %> </div> <% end %> <div class="form-inputs"> <%= f.input :email %> <%= f.input :password %> <%= f.input :password_confirmation %> </div> <div class="form-actions"> <%= f.button :submit %> </div> <% end %> 

Greetings

+6
source share
2 answers

The error message you provided indicates can't mass-assign protected attributes: profiles . I believe you need attr_accessible :profiles (or perhaps :profile )

I have an application with

 accepts_nested_attributes_for :order_items attr_accessible :order_item 
+5
source

The problem was in the rails class cache, I restarted the server and everything worked fine.,

+3
source

All Articles