18

哪个 gem 最适合 Rails 4 中的身份验证?我尝试使用设计,但我遇到了问题。

.rvm/gems/ruby-2.0.0-p0/gems/activemodel-4.0.0.beta1/lib/active_model/deprecated_mass_assignment_security.rb:14:in `attr_accessible': `attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your Gemfile to use old one. (RuntimeError)
    from /home/leapfrog/projects/kathloc/app/models/user.rb:8:in `<class:User>'
    from /home/leapfrog/projects/kathloc/app/models/user.rb:1:in `<top (required)>'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:423:in `load'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:423:in `block in load_file'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:615:in `new_constants_in'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:422:in `load_file'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:323:in `require_or_load'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:462:in `load_missing_constant'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:183:in `const_missing'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/inflector/methods.rb:226:in `const_get'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/inflector/methods.rb:226:in `block in constantize'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/inflector/methods.rb:224:in `each'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/inflector/methods.rb:224:in `inject'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/inflector/methods.rb:224:in `constantize'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:534:in `get'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:565:in `constantize'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise.rb:261:in `get'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/mapping.rb:77:in `to'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/mapping.rb:72:in `modules'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/mapping.rb:89:in `routes'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/mapping.rb:156:in `default_used_route'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/mapping.rb:66:in `initialize'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise.rb:291:in `new'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise.rb:291:in `add_mapping'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/rails/routes.rb:193:in `block in devise_for'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/rails/routes.rb:192:in `each'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/devise-1.5.4/lib/devise/rails/routes.rb:192:in `devise_for'
    from /home/leapfrog/projects/kathloc/config/routes.rb:2:in `block in <top (required)>'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:320:in `instance_exec'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:320:in `eval_block'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-4.0.0.beta1/lib/action_dispatch/routing/route_set.rb:298:in `draw'
    from /home/leapfrog/projects/kathloc/config/routes.rb:1:in `<top (required)>'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `load'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `block in load'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:222:in `load'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `each'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/file_update_checker.rb:75:in `call'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/file_update_checker.rb:75:in `execute'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:30:in `run'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /home/leapfrog/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application.rb:213:in `initialize!'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/leapfrog/projects/kathloc/config/environment.rb:5:in `<top (required)>'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:228:in `require'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:228:in `block in require'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:228:in `require'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/application.rb:187:in `require_environment!'
    from /home/leapfrog/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0.beta1/lib/rails/commands.rb:45:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
4

7 回答 7

26

这是因为在rails-4中,ActiveModel::MassAssignmentSecurity已从 ActiveModel 中取出。但是设计使用attr_accessible ,因此要使用它,只需在您的Gemfile中添加以下 gem并尝试再次加载。

gem 'protected_attributes'

编辑:

现在设计支持rails-4。将此包含在您的 Gemfile 中。

gem 'devise', '3.0.0.rc'
于 2013-03-20T10:21:35.863 回答
13

编辑:现在有一个支持 Rails 4 的设计版本:设计 3.0.0.rc

上一个答案:我最终为使其正常工作所做的事情是......

  1. 在 Gemfile 中: gem 'devise', github: 'plataformatec/devise', branch: 'rails4'

  2. 在生成的模型中,删除这些attr_accessible东西。

  3. 为设计启用强参数而不是 attr_accessible。为此,请使用该内容创建一个新的初始化程序:

    DeviseController.class_eval do
      def resource_params
        unless params[resource_name].blank?
          params.require(resource_name).permit(:email, :password, :password_confirmation, :remember_me)
        end
      end
    end
    
于 2013-03-26T00:43:42.550 回答
4

不要attr_accessible在您的项目中使用 with devise,请按以下方式使用 accesssibles -

就像我们有 attr_accessible :email, :password, :password_confirmation, :remember_me 一样,出于安全原因,我们可以将它们转换为私有方法:

private

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

end
于 2013-03-20T11:37:50.193 回答
1

这真的取决于你需要宝石做什么。有很多选择,Devise 绝对是顶级的。我建议通过 Thoughtbot 进行清除(https://github.com/thoughtbot/clearance)。它经过了很好的测试,并且经常更新。对我来说,当我需要核心身份验证功能而不是像 Devise 这样的 gem 附带的所有其他东西时,它非常有用!

于 2013-07-09T20:10:53.063 回答
0

在 GemFile 中写入 gem "devise", "~> 3.0.0.rc"。并按照此处提供的说明https://github.com/plataformatec/devise

我希望你的问题能得到解决。

于 2013-07-01T10:15:07.203 回答
0

如果您在使用 Devise 时遇到问题,请尝试使用“Authlogic”进行身份验证。

您可以使用 gem 'authlogic' '~3.2.0' 这是支持 Rails 4.0 的最新版本

Ruby 工具箱有一个最受欢迎的列表:http ://ruby-toolbox.com/categories/rails_authentication.html

于 2013-03-20T10:58:15.500 回答
0

我已经修复了 rails 4 设计中的问题。您现在可以毫不费力地实施它。

只需将 gem 'devise' 添加到您的 Gemfile 并进行捆绑。

谢谢

于 2015-05-06T13:09:10.733 回答