【问题标题】:Manually instantiating devise user models手动实例化设计用户模型
【发布时间】:2014-12-19 10:08:22
【问题描述】:

这个问题与我几年前问的一个问题有关:

Instantiating Devise user models manually using contents of params hash

我不确定这是否是 rails 4 问题,但我发现我无法在我的控制器代码中手动实例化设计用户。这曾经在 Rails 3 中工作。

class RegistrationsController < Devise::RegistrationsController

  ...
  def schema_test

    @user = User.new(:email => 'jhw@ausd.k12.edu', :password => 'asdf123', :password_confirmation => 'asdf123')
    @user.save
  end
  ...
end

这是我的 routes.rb 中特定于设计的部分: devise_for :users, :controllers => {:registrations => "registrations"}

devise_scope :user do
  get '/schema_test', to: 'registrations#schema_test'
end

当我调用 schema_test 时,我发现用户对象没有保存到数据库中。有人有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    最佳做法是在将 Rails 控制台集成到 Rails 应用程序之前检查其行为。

    找出问题的最简单方法是阅读设计返回的错误消息。这可能是由于未满足密码要求和/或电子邮件已存在于您的数据库中造成的。

    从应用程序的根目录运行rails c

    @user = User.new(:email => 'jhw@ausd.k12.edu', :password => 'asdf123', :password_confirmation => 'asdf123')
    
    # Check if the user object is valid
    @user.valid?
    
    # If it comes back false, read the error messages
    @user.errors.messages
    
    => {:password=>["is too short (minimum is 8 characters)"]} 
    

    在您的示例中,用户没有被保存,因为密码太短。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2017-08-30
      • 2019-05-31
      • 1970-01-01
      • 2023-03-05
      • 2013-06-23
      • 2013-05-12
      相关资源
      最近更新 更多