【问题标题】:Using devise scope to redefine default routes使用设计范围重新定义默认路由
【发布时间】:2013-07-27 16:59:33
【问题描述】:

尝试使用设计范围重新定义会话路由时出错。

devise_for :users
devise_scope :users do
  get    '/login'   => 'devise/sessions#new',     as: :new_user_session
  post   '/login'   => 'devise/sessions#create',  as: :user_session
  delete '/logout'  => 'devise/sessions#destroy', as: :destroy_user_session
end

错误。

Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes

我理解错误只是不知道它发生的原因。范围不会重新定义为 new_user_session 吗?

此外,如果您关注 wiki,您将收到关于传递块的设计弃用警告。 https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    我想我需要跳过会话 devise_for :users, :skip => [:sessions]

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。如果你注意 devise_scope 模型必须是单数和 devise_for 复数。所以解决办法是:

      devise_for :users
      devise_scope :user do
        ...
      end
      

      【讨论】:

        【解决方案3】:

        编辑:误读了这个问题。 如果您要覆盖设计控制器,您应该能够将 :controllers 选项哈希传递给 devise_for。可以让您的新控制器从原始控制器继承。

        如果你只是想改变路径,你不会想使用 devise_scope。在这种情况下应该可以使用以下方法:

          devise_for :users, :path => :account, :path_names => {sign_in: 'login', sign_out: 'logout', sign_up: 'register'} # 'account/login' replaces 'users/sign_in', etc.
        

        【讨论】:

          【解决方案4】:

          可能的解决方案是在 devise_for 中添加 "as" 选项

          devise_for :users, as: "some_prefix"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-07-27
            • 2016-09-10
            • 2015-12-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多