7

I have seen Ryan railcasts episode 274 I am using rails 4 and encountered one problem.

In password_resets_controller.rb

elsif @user.update_attributes(params[:user])

IN console it showing

ActiveModel::ForbiddenAttributesError in PasswordResetsController#update

when I modified update_attributes to update_attribute it shows

wrong number of arguments (1 for 2)

params[:user] showing two value password and password_confirmation but i am using password in my login page

I do not know how to solve this issue.

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
user2567129
  • 91
  • 1
  • 5
  • `update_attribute` is used to update single column and requires two parameters one is column name and second value for that column like `@user.update_attribute('password', params[:user][:password])` Ref:- http://stackoverflow.com/questions/2778522/rails-update-attribute-vs-update-attributes/2778671#2778671 – Salil Jul 10 '13 at 11:06
  • This is a good question and has a great answer. As the OP it's your job to flag it as the accepted answer, just click that checkmark beside it. – Peter Wooster Jun 16 '14 at 10:54

3 Answers3

32

This is because of Strong parameters feature in Rails 4. It will be raised when forbidden attributes are used for mass assignment.

You have to permit the attributes in your controller. Like this

@user.update_attributes(params.require(:user).permit(:password, :password_confirmation))
Santhosh
  • 28,097
  • 9
  • 82
  • 87
1

Had the identical issue - getting same error message when attempting to make any change to any of my resources from within Active Admin. Strong parameters were whitelisted correctly in the model's controller but it wasn't until after reviewing the documentation I realized that I needed to include the model attributes to be whitelisted in the model within app/admin/your_model.rb. Once I did this all worked correctly from within Active Admin.

app/admin/your_model.rb

ActiveAdmin.register Your_model do
  permit_params :attribute_1, :attribute_2, :attribute_3, :etc, :etc
end

This worked on my local server. After pushing changes to git and deploying to VPS it worked there as well. Make sure you restart your app. In one case I had to restart my instance. Hopefully this helps someone.

Phil_ish
  • 95
  • 1
  • 1
  • 9
0

I spent days wondering why mine was not working, yet i had done almost as every correct answer suggested. It was a legacy codebase and whoever wrote it back then decide to do it in the following manner:

def update_params
  params.require(:user).permit(:email, :password)

  params
end

# And it was being called in the following manner
@user.update(update_params[:user])

I laboured for days before I found @Santhosh's answer above, tried it on the console and it worked.

The only fix that I had to make was to remove the return params in the update_params method, and only pass update_params to the call to update itself.

def update_params
  params.require(:user).permit(:email, :password)
end

# And it was being called in the following manner
@user.update(update_params) # Remove [:user]
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39