我想在用户选择卖家时重定向,当用户 select 买家进入主页时,它应该转到仪表板

i want redirect when user sellect seller it should go to the dashboard when user select buyer it go the homepage

这是我的用户模型

 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" %>

我不知道该怎么做,我尝试了很多方法,但它对我不起作用。我想做的是当用户select买家应该去主页当用户select卖家它应该去主页

您需要做的第一件事是创建 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' }