用户的未定义方法attr_accessible错误

我正在尝试创建各种登录。 我创建了一个用户脚手架,并在我的user.rb中包含此代码

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

我一直收到这个错误

 undefined method `attr_accessible' for # 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 

attr_accessible不适用于Rails version 4+ 。 你必须使用强大的参数。

使用强参数,属性白名单已移至控制器级别。 从模型中删除attr_accessible调用。

以下是Rails指南中有关如何使用强参数的示例

在你的情况下,你可以做这样的事情:

 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 

您可以在我的回答下方记下@Frederick评论,

你仍然可以使用attr_accessible但它已被提取到protected_attributes gem中(尽管明显强大的参数是向前的方式)