【问题标题】:Confusing about a function :before_filter对功能感到困惑:before_filter
【发布时间】:2011-12-29 23:31:26
【问题描述】:

我有以下控制器:

class CarsController < ApplicationController
  autocomplete :user, :name
  before_filter :require_user, :except => [:my_action]

  def index
  end

  ...

  def my_action
  end
end

我想只允许登录用户查看此控制器中的所有操作 - 这对我来说很好。但是my_action 的操作我希望每个人都可以访问 - 也适用于未登录的人。

我尝试使用:except 参数设置:before_filter,也使用:only 参数,但没有任何效果...该应用程序总是希望我登录...我还在做什么错了吗?

编辑require_user 来自 application_controller.rb:

   def require_no_user
      logger.debug "ApplicationController::require_no_user"
      if current_user
        #store_location
        flash[:warning] = "You must be logged out to access this page"
        redirect_to account_url
        return false
      end
    end

【问题讨论】:

  • require_user 是什么?它是从哪里来的?
  • 也许这只是一个错字...但dev my_action 应该是def my_action ...在您的实际代码中是这样输入的吗?
  • 另外,您的应用程序控制器中有过滤器吗?如果是这样,您需要在此控制器中跳过它。
  • @JustinM - 当然,在我的代码中是def,而不是dev,这是我在输入此消息时犯的错误。
  • 正如你的方法现在写的那样,如果有current_user,它会返回false ...这是你想要的吗?

标签: ruby authentication ruby-on-rails-3.1 before-filter


【解决方案1】:

使用 skip_before_filter :require_user, :only => [:my_action]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2013-08-21
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多