簡體   English   中英

如何創建沒有密碼的用戶並稍后使用設計表單進行設置?

[英]How to create user without password and set it later with Devise form?

我正在構建一個 Rails 應用程序,我想在 Rails 控制台上創建沒有密碼的用戶,收到一封確認電子郵件,然后單擊確認電子郵件中的鏈接,在我的網站上設置密碼。 (我正在使用設計)

這是我到目前為止嘗試過的:

應用程序/模型/user.rb

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

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

應用程序/控制器/用戶/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)因為我希望人們在這個過程中登錄。

應用程序/控制器/users_controller.rb

class UsersController < ApplicationController
    def create
    end
end

目前,當我通過 rails 控制台創建一個用戶然后單擊確認鏈接時,我最終進入了設計視圖,以編輯我的帳戶(更具體地說是我的密碼),這很棒,但我無法驗證表格,因為我必須填寫我以前的密碼才能更改它。 但是由於我在創建過程中沒有設置任何密碼,所以我被卡住了!

關於我如何做到這一點的任何想法?

謝謝

編輯

正如評論中提到的,我嘗試使用“忘記密碼”鏈接。 它有效,但在設置密碼后,用戶必須登錄(因此再次輸入他們的密碼)。 在我看來,這可能不是一個很好的客戶體驗,這就是為什么我想知道是否有辦法像我在我的帖子中解釋的那樣做,或者在用戶設置密碼后登錄的方法第一次。

更新

經過一些建議,我對文件進行了一些更改,但仍然收到錯誤消息,指出當前密碼不能為空。 這是我的代碼:

路由文件

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

應用程序/控制器/用戶/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

用戶名

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)

告訴我您是否還有其他想要查看的文件。

module Users
  class RegistrationsController < ::Devise::ConfirmationsController
    protected
    # By default Devise requires a password check on update.
    # this override checks if there is stored password so that
    # a confirmed user without a password can add their password
    def update_resource(resource, params)
      if resource.encrypted_password.present?
        super
      else
        resource.update(params)
      end
    end
  end
end

update_resourceDevise::ConfirmationsController#update調用。 默認情況下,它調用resource.update_with_password(params) ,它會與參數混在一起,如果當前密碼無效,則會添加驗證錯誤。 它不受password_required? 因為這個特殊的“驗證”是作為控制器流程的一部分完成的。

如果沒有保存密碼,這只是繞過它。

您還需要配置路由:

devise_for :users, controllers: {
  confirmations: 'users/confirmations',
  registrations: 'users/registrations'
}

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

它為您提供了一個帶有表單的/users/invitation/new路徑,您可以填寫該表單來邀請用戶。 用戶記錄被保存,然后用戶通過接受邀請完成注冊過程。

如果你真的想要,你可以從控制台發送邀請:

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

但實際上,我只會使用 Pundit 或 CanCanCan 設置一些基本授權來鎖定邀請控制器並通過 GUI 執行此操作。 無論如何,您很可能會需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM