ruby-on-rails - 如何创建没有密码的用户并稍后使用 Devise 表单进行设置?

标签 ruby-on-rails ruby devise

我正在构建一个 Rails 应用程序,我想在没有密码的 Rails 控制台上创建用户,收到一封确认电子邮件,然后单击确认电子邮件中的链接,在我的网站上设置密码。 (我正在使用 Devise)

到目前为止,这是我尝试过的:

app/models/user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable

  protected
  def password_required?
    confirmed? ? super : false
  end
end

app/controllers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController
  protected
  def after_confirmation_path_for(resource_name, resource)
    sign_in(resource)
    edit_registration_path(resource)
  end
end

我特地做了 sign_in(resource) 因为我希望人们在这个过程中登录。

app/controllers/users_controller.rb

class UsersController < ApplicationController
    def create
    end
end

目前,当我通过 Rails 控制台创建用户然后单击确认链接时,我最终会进入设计 View ,以编辑我的帐户(更具体地说是我的密码),这很好,但我不能不要验证表格,因为我必须填写我以前的密码才能更改它。但是由于我在创建过程中没有设置任何密码,所以我卡住了!

关于我如何做到这一点有什么想法吗?

谢谢

编辑

如评论中所述,我尝试使用“忘记密码”链接。它有效,但在设置密码后,用户必须登录(因此再次输入密码)。在我看来,这可能不是一个很好的客户体验,这就是为什么我想知道是否有一种方法可以做到这一点,就像我在我的帖子中解释的那样,或者一种在用户设置密码后登录的方法第一次。

更新

根据一些建议,我对我的文件进行了一些更改,但我仍然收到错误消息,提示当前密码不能为空。这是我的代码:

routes.rb

Rails.application.routes.draw do
  root to: 'page#index'

  devise_for :users, path: '', path_names: { sign_in: 'sign_in', sign_out: 
  'sign_out'}, controllers: { confirmations: 'users/confirmations', 
  registrations: 'users/registrations' }
end

app/controllers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController
  def update_resource(resource, params)
    if resource.encrypted_password.present?
      super
    else
      resource.update(params)
    end
  end

  protected
  def after_confirmation_path_for(resource_name, resource)
    sign_in(resource)
    edit_registration_path(resource)
  end
end

用户.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable

  protected
  def password_required?
    confirmed? ? super : false
  end
end

app/views/devise/registrations/edit.html.erb

<h2>Edit your account</h2>

<div>
  <%= devise_error_messages! %>
  <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
    <p><%= current_user.first_name %> <%= current_user.last_name %></p>
    <p>Email address: <strong><%= current_user.email %></strong></p>

    <div class="container mb-5">
      <div class="row">
        <%= f.label :password %>
      </div>
      <div class="row">
        <%= f.password_field :password, autofocus: true, class: 'form-control', :required => 'required' %>
      </div>
    </div>

    <div class="container mb-5">
      <div class="row">
        <%= f.label :password_confirmation %>
      </div>
      <div class="row">
        <%= f.password_field :password_confirmation, autofocus: true, class: 'form-control', :required => 'required' %>
      </div>
    </div>



    <div class="container text-center mb-3">
      <%= f.submit "Update", class: 'navbar-cta' %>
    </div>
  <% end %>
</div>

日志

Started GET "/edit" for ::1 at 2020-01-20 18:06:29 +0100
Processing by Users::RegistrationsController#edit as HTML
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 33], ["LIMIT", 1]]
  Rendering devise/registrations/edit.html.erb within layouts/application
DEPRECATION WARNING: [Devise] `DeviseHelper.devise_error_messages!`
is deprecated and it will be removed in the next major version.
To customize the errors styles please run `rails g devise:views` and modify the
`devise/shared/error_messages` partial.
 (called from _app_views_devise_registrations_edit_html_erb___445667363343301985_70311530657080 at /Users/victor/Documents/SaaS projects/ChurnTarget/app/views/devise/registrations/edit.html.erb:6)
  Rendered devise/registrations/edit.html.erb within layouts/application (Duration: 7.3ms | Allocations: 1979)
  Rendered layouts/_google_analytics.html.erb (Duration: 0.4ms | Allocations: 164)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered page/_navbar.html.erb (Duration: 1.9ms | Allocations: 669)
  Rendered page/_footer.html.erb (Duration: 0.8ms | Allocations: 162)
Completed 200 OK in 226ms (Views: 221.6ms | ActiveRecord: 0.5ms | Allocations: 61076)


Started PUT "/" for ::1 at 2020-01-20 18:07:00 +0100
Processing by Users::RegistrationsController#update as HTML
  Parameters: {"authenticity_token"=>"99pJ5XaS5k5NQmba31GrTu5+jeN57mdPV51XlG6WFJoizS/5rbeLerzmTQv+kbsPIPorjH9fjAz3ihPxXENo1w==", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 33], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 33], ["LIMIT", 1]]
Unpermitted parameter: :password_confirmation
  Rendering devise/registrations/edit.html.erb within layouts/application
DEPRECATION WARNING: [Devise] `DeviseHelper.devise_error_messages!`
is deprecated and it will be removed in the next major version.
To customize the errors styles please run `rails g devise:views` and modify the
`devise/shared/error_messages` partial.
 (called from _app_views_devise_registrations_edit_html_erb___445667363343301985_70311530657080 at /Users/victor/Documents/SaaS projects/ChurnTarget/app/views/devise/registrations/edit.html.erb:6)
  Rendered devise/shared/_error_messages.html.erb (Duration: 2.0ms | Allocations: 441)
  Rendered devise/registrations/edit.html.erb within layouts/application (Duration: 7.5ms | Allocations: 1514)
  Rendered layouts/_google_analytics.html.erb (Duration: 0.1ms | Allocations: 8)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered page/_navbar.html.erb (Duration: 0.1ms | Allocations: 15)
  Rendered page/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
Completed 200 OK in 208ms (Views: 40.7ms | ActiveRecord: 1.1ms | Allocations: 20837)

如果您还想查看其他文件,请告诉我。

最佳答案

完全不同的解决方案是使用 Devise::Invitable提供您可能正在寻找的功能。

它为您提供了一个 /users/invitation/new 路径,其中包含一个表单,您可以填写该表单来邀请用户。保存用户记录,然后用户通过接受邀请完成注册过程。

如果您真的想要,您可以从控制台发送邀请:

User.invite!(email: 'someone@example.com')

但实际上我只想使用 Pundit 或 CanCanCan 设置一些基本授权来锁定邀请 Controller 并通过 GUI 完成。无论如何,您很可能会需要它。

关于ruby-on-rails - 如何创建没有密码的用户并稍后使用 Devise 表单进行设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59826297/

相关文章:

mysql - 结合rails中两个表的数据

ruby-on-rails - devise 允许任何人通过其电子邮件地址查找用户!如何防止这种情况?

javascript - Rails - 为什么 ajax 不发送参数到 GET 请求

ruby-on-rails - 设计,什么是/my_users/password/edit

ruby - 在 rspec 中禁用一组测试?

ruby - 如何引用当前线程

ruby-on-rails - 更新旧记录而不是创建新记录?

ruby-on-rails - Rails 4.2 中未定义的方法 'authenticate_with_http_basic'

ruby-on-rails - 目录 : Forgot Password & Sign In - in same page

ruby-on-rails - 如何在设计生成的注册表中添加一个额外的字段?