【问题标题】:Trying to set root to devise/sessions#new results in routes mapping error尝试将 root 设置为 devise/sessions#new 会导致路由映射错误
【发布时间】:2012-01-15 22:03:30
【问题描述】:

在这里遇到问题会发疯:-(

我的要求如下,

  1. 如果用户访问 myapp.com,我的应用程序的根目录默认为促销页面和注册表单。这是通过检查子域的存在来实现的。

  2. 如果用户未登录并尝试访问他们在 test.myapp.com 上的帐户,他们将被定向到 test.myapp.com/users/sign_in -aka-devise/sessions#new

  3. 如果用户已登录(设计)并访问 test.myapp.com,则应用程序的根目录将是应用程序仪表板。

这是我试图在我的 routes.rb 中使用的内容

constraints(Subdomain) do
  authenticated do
    root :to => 'dashboard#index'
  end
  root :to => 'devise/sessions#new'
end
root :to => 'promo_pages#index'

目前我有以下,你会注意到设计位不包括在内。

constraints(Subdomain) do
  authenticated do
    root :to => 'dashboard#index'
  end
end
root :to => 'promo_pages#index'

我对后者的问题是,当未登录的用户首次访问 test.myapp.com 时,他们会被重定向到 test.myapp.com/users/sign_in 并显示错误消息“您需要签名”在继续之前输入或注册。”这是因为我正在执行仪表板页面的登录要求。

但是我不希望用户在第一次访问该页面时收到错误消息,因为它很丑陋,并且看起来他们好像做错了什么,而实际上他们没有做错。

我的期望是,如果用户没有登录,那么他们将被直接引导到登录页面并且不会收到错误通知。但是当我使用修改后的版本时,会发生以下情况, 我可以很好地访问 myapp.com,它被路由到促销页面 但如果我尝试访问 test.myapp.com,我会在浏览器中收到以下消息

**Unknown action**
Could not find devise mapping for path "/". Maybe you forgot to wrap your route inside the scope block? For example: devise_scope :user do match "/some/route" => "some_devise_controller" end 

请告知我做错了什么(如果有的话),因为我正在努力了解该怎么做。

Ps:我在 stackoverflow 和各种谷歌搜索中发现了类似的错误,但解决方案似乎对我不起作用。我希望问题的解决方案在于我上面包含的错误消息,但我不知道如何应用它。

最后是上面错误的日志入口版本,它是一个更容易阅读的形式。

Started GET "/" for 127.0.0.1 at 2012-01-15 21:44:42 +0000
  Processing by Devise::SessionsController#new as HTML
[Devise] Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:

    devise_scope :user do
      match "/some/route" => "some_devise_controller"
    end
Completed 404 Not Found in 1ms

AbstractController::ActionNotFound (Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:

    devise_scope :user do
      match "/some/route" => "some_devise_controller"
    end
):

感谢所有帮助,并且可以提供更多详细信息。

感谢阅读


更新

我刚刚注意到“已验证”检查似乎不起作用。 如果它工作正常,那么在使用第二批工作配置时,访问 test.myapp.com 的登录用户将始终被定向到 promo_pages,而此时他能够访问仪表板..

我在这里找到了设计“认证”方法 https://github.com/plataformatec/devise/pull/1147

【问题讨论】:

  • 这个问题现在已经解决了,但是我不能再发布 4 小时的答案,所以我会尽可能地做。

标签: ruby-on-rails devise routes


【解决方案1】:

如果没有约束,这将导致找不到路径“/”的设计映射错误。这个简单的添加修复了它。

devise_scope :user do 
    authenticated :user do
      root :to => 'dashboard#index'
    end
    unauthenticated :user do
      root :to => 'devise/sessions#new'
    end
    root :to => 'dashboard#index'
end

【讨论】:

    【解决方案2】:

    我已经整理好了,

    好的,首先我收到设计错误的原因是我需要将设计根语句放在“devise_for :users”块中

    其次, 验证检查不起作用,因为我没有包含范围,因为我错误地认为没有必要。

    这是最终的代码,注意在rails路由中优先级是基于创建的顺序,首先创建的优先级最高。因此,在这种情况下,promo_pages 控制器仅在之前未指定任何其他内容的情况下才被视为 root。

    constraints(Subdomain) do
      authenticated :user do
        root :to => 'dashboard#index'
      end
    
      unauthenticated :user do
        root :to => 'devise/sessions#new'
      end
    end
    root :to => 'promo_pages#index'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多