Undefined update_attributes method in Rails 4

Now I am using Rails 4 Now I want to update my user account I'm trying to use this command

@user=User.update_attributes(:name=> params[:name], :user=> params[:username], :pass=> params[:password]) OR @user=User.update_attributes(name: params[:name], user: params[:username], pass: params[:password]) 

but always got an error

 undefined method `update_attributes' 

since I can update my user also I want to ask if he will update all users in my database?

I think I should add some condition, such as where id=@user.id , but I don’t know how to do this in rails !!!

+6
source share
1 answer

update_attributes is an instance method, not a class method, so you must first call it on an instance of the User class.

Get the user you want to update: for example, Suppose you want to update the user with id 1

  @user = User.find_by(id: 1) now if you want to update the user name and password, you can do 

or

  @user.update(name: "ABC", pass: "12345678") 

or

  @user.update_attributes(name: "ABC", pass: "12345678") 

Use it accordingly in your case.

For more information, you can refer to Ruby on Rails Guides .

You can use the update_all method to update all records. This is a class method, so you can name it as The following code will update the name of all user entries and set it to "ABC" User.update_all (name: "ABC")

+16
source

All Articles