Undefined attr_accessible error method for user

I am trying to create a login. I created a custom scaffold and got this code in my user.rb

class User < ActiveRecord::Base
attr_accessible :name,  :password_digest, :password, :password_confirmation

has_secure_password
end

I keep getting this error

undefined method `attr_accessible' for #<Class:0x396bc28>

Extracted source (around line #2):
1
2
3
4
5

class User < ActiveRecord::Base
  attr_accessible :name,  :password_digest, :password, :password_confirmation

  has_secure_password
end

Rails.root: C:/Sites/web
+4
source share
1 answer

attr_accessibleunavailable for Rails version 4+. You will have to go with strong parameters.

With strong parameters, the attribute attribute attribute has moved to the controller level. Remove the call attr_accessiblefrom your model.

Here is an example in the Rails Guide on how to use Strong Parameters

In your case, you can do something like this:

class UsersController < ApplicationController
  ## ...
  def create
    @user = User.new(user_params) ## Invoke user_params method
    if @user.save
      redirect_to @user, notice: 'User was successfully created.' 
    else
      render action: 'new'
    end       
  end
  ## ... 

  private
  ## Strong Parameters 
  def user_params
    params.require(:user).permit(:name, :password_digest, :password, :password_confirmation)
  end
end 

You can take note of @Frederick's comment below my answer,

attr_accessible, protected_attributes ( )

+16

Source: https://habr.com/ru/post/1541151/


All Articles