3

I'm upgrading from Rails 3.2 to 4.0 and am using Devise for my User model. Previously I had attr_accessible attributes in my User model:

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

Now according to the Devise documentation for strong parameters, I have to do the following in my application_controller.rb in a before_filter method:

devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :first_name, :last_name, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :current_password) }

That seems fine, but I also have an accepts_nested_attributes_for in my User model:

accepts_nested_attributes_for :staff, reject_if: :new_record?

How do I specify that in my devise_parameter_sanitizer.for(:account_update)? This is not clear to me.

UPDATE: I tried the following:

devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :current_password, staff_attributes: [:notification_email]) }

In particular, I added staff_attributes: [:notification_email] as a parameter to the permit method. 3 major problems happened when I simply saved a user edit form without making any changes:

  1. Error displayed: "Staff notification email has already been taken" (I have validates :notification_email, uniqueness: true, allow_nil: true in the Staff model)
  2. Error displayed: "Staff project can't be blank" (I have validates_presence_of :project_id in the Staff model)
  3. Even though I received those 2 errors, the staff model associated with the user was wiped out, all attributes were set to nil except the notification_email. This included the user_id field, so the staff object was no longer connected to the user.

1 Answer 1

2

These are nested attributes, so you need to add them in following way:

devise_parameter_sanitizer.for(:sign_up) { |u|
  u.permit(:username, :staff_attributes => [:name, :position, :etc]) 
}
2
  • Wow, that was surprising. I added this: , staff_attributes: [:notification_email] to the permit call. Because I only want users to be able to update their notification_email if they're a staff member. What ended up happening is all other fields, including the user_id field of Staff were set to nil! So the user is no longer a staff member! Is there a way to only be able to update 1 of the staff attributes? Or at least not set all other fields to nil? Also, when I edited the user I got an error that another field of staff was required, even though it still niled all fields.
    – at.
    Mar 14, 2014 at 9:54
  • 1
    The problem was I didn't have :id as one of the elements in the array pointed to by staff_attributes. Now everything works perfect!
    – at.
    Mar 14, 2014 at 19:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.