Rails。 Devise 的 2 个用户模型的路由问题

Rails. Routing problem with 2 User models for Devise

我有两个模型,会计和客户。我想用 Devise 进行身份验证。我有以下配置:

在路线中

Rails.application.routes.draw do
  # devise_for :accountants, path: 'accountants'
  devise_for :accountants, path: 'accountants', controllers: { registrations: 'accountants/registrations', sessions: 'accountants/sessions' }

  root to: 'pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  get "registration", to: "pages#registration"
end

Accountant.rb

class Accountant < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  belongs_to :company
end

accountants/new.html.erb

注册会计师

<%= simple_form_for(resource, as: resource_name, url: accountant_registration_path) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true ,
                input_html: { autocomplete: "email" }%>
    <%= f.input :password,
                required: true,
                hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :password_confirmation,
                required: true,
                input_html: { autocomplete: "new-password" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "accountants/shared/links" %>

我用过:

rails generate devise:controllers accountants

创建以下控制器

# frozen_string_literal: true

class Accountants::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    super
  end

  # POST /resource
  def create
    super
  end

点击注册后,我被重定向到 /accountants url,在那里我看到了带有验证错误的注册表单。最少 6 个字符(并显示电子邮件),(我输入了一个 6 个字符的密码)。

任何关于如何调试的帮助?

路线如下:

Prefix Verb   URI Pattern                                                                              Controller#Action
        new_accountant_session GET    /accountants/sign_in(.:format)                                                           accountants/sessions#new
            accountant_session POST   /accountants/sign_in(.:format)                                                           accountants/sessions#create
    destroy_accountant_session DELETE /accountants/sign_out(.:format)                                                          accountants/sessions#destroy
       new_accountant_password GET    /accountants/password/new(.:format)                                                      devise/passwords#new
      edit_accountant_password GET    /accountants/password/edit(.:format)                                                     devise/passwords#edit
           accountant_password PATCH  /accountants/password(.:format)                                                          devise/passwords#update
                               PUT    /accountants/password(.:format)                                                          devise/passwords#update
                               POST   /accountants/password(.:format)                                                          devise/passwords#create
cancel_accountant_registration GET    /accountants/cancel(.:format)                                                            accountants/registrations#cancel
   new_accountant_registration GET    /accountants/sign_up(.:format)                                                           accountants/registrations#new
  edit_accountant_registration GET    /accountants/edit(.:format)                                                              accountants/registrations#edit
       accountant_registration PATCH  /accountants(.:format)                                                                   accountants/registrations#update
                               PUT    /accountants(.:format)                                                                   accountants/registrations#update
                               DELETE /accountants(.:format)                                                                   accountants/registrations#destroy
                               POST   /accountants(.:format)                                                                   accountants/registrations#create
                          root GET    /                                                                                        pages#home
                  registration GET    /registration(.:format)                                                                  pages#registration

尝试像下面这样更改url您的注册表单,如果它不起作用请告诉我

<%= simple_form_for(resource, as: resource_name, url: account_registration_url(resource_name)) do |f| %>

尝试更改 URL 注册表单如下:

<%= simple_form_for(resource, as: resource_name, url: accountant_registration_path) do |f| %>