Ruby-on-rails 未定义的方法 attr_accessible

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23437830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:36:06  来源:igfitidea点击:

Undefined Method attr_accessible

ruby-on-railsruby

提问by Cameron Jones

I am somewhat new to rails and I am trying to create a User login. I went through the tutorial found here. At the end it had me add "attr_accessible" for mass assignment. However when I did that I got the following error:

我对 rails 有点陌生,我正在尝试创建一个用户登录。我浏览了此处找到的教程。最后它让我添加“attr_accessible”进行批量分配。但是,当我这样做时,出现以下错误:

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

I saw on this postthat I neeed < ActiveRecord::Base. But I do have that included. Here is the code for my User Model:

我在这篇文章中看到我需要 < ActiveRecord::Base。但我确实包括在内。这是我的用户模型的代码:

class User < ActiveRecord::Base

  attr_accessor :password
  EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
  before_save :encrypt_password
  after_save :clear_password
  attr_accessible :username, :email, :password, :password_confirmation

  def encrypt_password
    if password.present?
      self.salt = BCrypt::Engine.generate_salt
      self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
    end
  end

  def clear_password
    self.password = nil
  end

end

Any other ideas on what could be causing this problem would be really appreciated, thanks!

关于可能导致此问题的任何其他想法将不胜感激,谢谢!

Edit: On Rails 4.1. Looks like it doesn't apply anymore. Thanks fotanus

编辑:在 Rails 4.1 上。好像已经不适用了。谢谢 fotanus

回答by Wally Ali

No mass assignment allowed for Rails 4.1

Rails 4.1 不允许批量分配

instead of having attr_accessible :username, :email, :password, :password_confirmationin your model, use strong parameters. You'll do this in your UsersController:

而不是attr_accessible :username, :email, :password, :password_confirmation在你的模型中,使用强参数。您将在您的 UsersController 中执行此操作:

    def user_params
      params.require(:user).permit(:username, :email, :password, :password_confirmation)
    end

then call the user_params method in your controller actions.

然后在您的控制器操作中调用 user_params 方法。

回答by Sarvesh

No mass assignment allowed for Rails 4.1

Rails 4.1 不允许批量分配

You will have to try something like this.

你将不得不尝试这样的事情。

class Person
  has_many :pets
  accepts_nested_attributes_for :pets
end

class PeopleController < ActionController::Base
  def create
    Person.create(person_params)
  end

  ...

  private

    def person_params
      # It's mandatory to specify the nested attributes that should be whitelisted.
      # If you use `permit` with just the key that points to the nested attributes hash,
      # it will return an empty hash.
      params.require(:person).permit(:name, :age, pets_attributes: [ :name, :category ])
    end
end

Refer

参考

https://github.com/rails/strong_parameters

https://github.com/rails/strong_parameters

回答by S? Ph?m

Make sure you are installed gem 'protected_attributes', that this gem is present in your gemfile and run bundle installterminal. Then restart your server.

确保您已安装 gem 'protected_attributes',该 gem 存在于您的 gemfile 中并运行bundle install终端。然后重启你的服务器。