ruby 如何创建没有密码的用户,并在以后使用Devise表单设置它?

bwntbbo3  于 2023-02-12  发布在  Ruby
关注(0)|答案(2)|浏览(105)

我正在构建一个Rails应用程序,我想在Rails控制台上创建一个没有密码的用户,收到一封确认邮件,然后点击确认邮件中的链接,在我的网站上设置密码。(我使用Devise)
以下是我目前尝试的方法:

    • 应用程序/型号/用户. rb**
class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable

  protected
  def password_required?
    confirmed? ? super : false
  end
end
    • 应用程序/控制器/用户/确认_控制器. 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),因为我希望人们在此过程中登录。

    • 应用程序/控制器/用户_控制器. rb**
class UsersController < ApplicationController
    def create
    end
end

目前,当我通过rails控制台创建一个用户,然后点击确认链接,我会在devide视图中结束,编辑我的帐户(更确切地说是我的密码),这很好,但我不能验证表单,因为我必须填写我以前的密码来修改它,但由于我在创建过程中没有设置任何密码,我卡住了!
你知道我是怎么做到的吗?
谢谢

    • 编辑**

正如评论中提到的,我尝试使用"忘记密码"链接。它工作,但设置密码后,用户将不得不登录(所以再次输入他们的密码)。在我看来,这可能不是一个很好的客户体验,这就是为什么我想知道是否有一种方法可以做到这一点,因为我解释了我的帖子,或者在用户第一次设置密码后登录用户的方法。

    • 更新**

经过一些建议后,我对文件做了一些修改,但仍然收到错误消息,说当前密码不能为空。下面是我的代码:

    • 路由. 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
    • 应用程序/控制器/用户/确认_控制器. 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
    • 应用程序/视图/设计/注册/编辑. 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)

告诉我您是否还想查看其他文件。

58wvjzkj

58wvjzkj1#

一个完全不同的解决方案是使用Devise::Invitable,它提供了您可能正在寻找的特性。
它给你一个/users/invitations/new路径,你可以填写一个表单来邀请用户,用户记录被保存,然后用户通过接受邀请来完成注册过程。
如果您真的想这样做,您可以使用以下命令从控制台发送邀请:

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

但实际上,我只想用Pundit或CanCanCan设置一些基本的授权来锁定邀请控制器,并通过GUI来完成。

zzlelutf

zzlelutf2#

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'
}

相关问题