Rails 6 - 如何替换路由 URI 模式的一部分?

Rails 6 - How to substitute a part of the routes URI pattern?

如果用户调用 URL application.com,它会重定向到 申请.com/account/1/sessions/new.

我想将重定向 URL 替换为 应用程序。com/sessions/new (host/account/:id -> host)

routes.rb

Rails.application.routes.draw do

get '/', to: 'dashboard#index'

  resources :accounts do
    resources :sessions
  end

end

rails 路线:

new_account_session GET    /accounts/:account_id/sessions/new(.:format)      sessions#new

application_controller.rb

class ApplicationController < ActionController::Base

...
  #account has a column host to be identified if the user called URL host belongs to an existing account
  def get_account_id
    account_id = Account.find_by(host: request.host).id
  end

  def authorize
    if session[:user_id]
      redirect_to new_account_session_path(get_account_id)
      return
    end
  end

end

从 :accounts 中删除 :sessions

resources :accounts
resources :sessions

当你像那样嵌套资源时,你基本上是在说所有到会话的路由都应该链接到一个帐户。