這是我的用戶模型
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: { Buyer: 0, Seller: 0 }
end
這是路線。
Rails.application.routes.draw do
devise_for :users
root 'home#homepage'
get 'dashboard', to: 'home#dashboard'
end
這是我的注冊表
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<%= f.label :role %>
<%= f.select :role, User.roles.keys %>
<div class="actions">
<%= f.submit "Sign up", data: {turbo: false} %>
</div>
<% end %>
<%= render "devise/shared/links" %>
我不知道該怎么做,我嘗試了很多方法,但它對我不起作用。我想做的是當用戶選擇買家時它應該轉到主頁當用戶選擇賣家時它應該轉到主頁
uj5u.com熱心網友回復:
您需要做的第一件事是創建一個registrations_controller并自定義after_sign_up_path_for(resource如下:
# app/controllers/users/registrations_controller.rb
module Users
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
# here you can check if the user is a buyer or seller and redirect to the correct path.
if current_user.buyer?
root_path
else
dashboard_path
end
end
end
end
之后,您需要編輯您的路線,以便設計將使用此控制器。
# app/config/routes.rb
devise_for :users, controllers: { registrations: 'users/registrations' }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427214.html
