【问题标题】:How to specify what controllers should be used when creating routes?创建路由时如何指定应该使用哪些控制器?
【发布时间】:2021-09-20 20:33:00
【问题描述】:

我正在创建一个 Rails 应用程序,我试图不使用 Devise 来处理与我的 User 模型相关的注册和会话,但我遇到了问题。

我想要这样的特定路线:

  • http://localhost:3000/users/registrations/new
  • http://localhost:3000/users/sessions/new

我尝试了几种解决方案,但都没有奏效,最后尝试的是那些代码行:

get "users/registrations/new", to: "registrations#new", controller: "users/registrations", as: :new_registration
get "users/sessions/new", to: "sessions#new", controller: "users/sessions", as: :new_session

但我收到以下错误:uninitialized constant RegistrationsController

我的registrations_controller 和我的sessions_controller 位于以下路径:app/controllers/users/

有谁知道我该如何处理?我应该使用resources 还是namespace?我已经尝试过这些解决方案,但我一直面临同样的问题......

感谢您的帮助!

【问题讨论】:

标签: ruby-on-rails authentication routes registration


【解决方案1】:

在你的情况下,你使用to: "namespace/name_of_controller#action"

get "users/registrations/new", to: "users/registrations#new", as: :new_registration
get "users/sessions/new", to: "users/sessions#new", as: :new_session

另一种方式,更“Rails-y”,是使用namespace

namespace :users do
  resources :registrations, only: [:new] # If you want only the new action
  # ...
end

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多