2017-08-10 66 views
0

我正在运行设计。除了特定路线之外,我希望所有内容都可以强制登录。在我的应用程序中,我保存了一个很长的URL并使用一个简短的URL指向我的应用程序以获取重定向的长URL。未登录的外部用户需要能够追踪该短网址并获取外部重定向的长网址。例如localhost.com/WdeFrs从数据库中拉长链接并重定向到以.... google.com为例。一切正常,除非你按照链接返回它强制登录。 它需要工作,无需登录有人可以帮助澄清修复。谢谢设计before_action:验证异常

application_controller.rb

before_action :authenticate_user!, :except => [:urls] 

的routes.rb

resources :urls 
    get "/:short_url", to: "urls#shortcustom" 
    get "shortened/:short_url", to: "urls#shortened", as: :shortened 
+0

您不希望登录用户使用哪种方法? – Vishal

+0

得到“/:short_url”,以:“urls#shortcustom”我想是我想要例外。 – Jrinard

+0

请在您的网址控制器中尝试此“skip_before_filter:authenticate_user !,:only =>:shortcustom”。 – Vishal

回答

1

before_action选项except期望一个动作名称,而不是一个控制器名称。原因是,您的代码正在跳过名为urls的任何操作的身份验证。

要跳过身份验证urls#shortcustomurls#shortened,你需要包括

skip_before_action :authenticate_user!, :only => [:shortcustom, :shortened] 

UrlsController

+0

我确实尝试过,并且出现错误:在skip_callback中:在process_action回调之前:authenticate_user!尚未定义(ArgumentError)我现在正在寻找修复此错误的方法。 :) – Jrinard

+0

@Jrinard如错误所述,在控制器的范围内没有定义'authenticate_user!'。你确定它是在'ApplicationController'中定义的,并且'UrlsController'继承了它吗? – vijoc

+0

是的,谢谢你我没有before_action:authenticate_user!在我的application_controller! 感谢您的帮助! – Jrinard