【问题标题】:Rails 3 - Filter chain halted as :authentication rendered or redirectedRails 3 - 过滤器链因 :authentication 呈现或重定向而停止
【发布时间】:2012-09-19 19:45:26
【问题描述】:

我仍然在标题中收到“错误”消息,不知道如何解决它。 在ApplicationController中,

class ApplicationController < ActionController::Base
   protect_from_forgery
   before_filter :mailer_set_url_options
   helper_method :current_user_session, :current_user

   def mailer_set_url_options
     ActionMailer::Base.default_url_options[:host] = request.host_with_port
   end

   private
      def current_user_session
        logger.debug "ApplicationController::current_user_session"
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = UserSession.find
      end

      def current_user
        logger.debug "ApplicationController::current_user"
        return @current_user if defined?(@current_user)
        @current_user = current_user_session && current_user_session.user
      end

      def authentication
        logger.debug "ApplicationController::authentication"
        unless current_user
          store_location
          flash[:warning] = "You must be logged out to access this page"
          redirect_to root_url
          return false
        end
      end

      def store_location
        session[:return_to] = request.url
      end
end

routes.rb

  #match 'set_activity_account/:id/:value' => 'users#account_activity', :as => :set_activity_account -- this doesn't work as well..
  resources :users do
    member do
      get :action_a, :action_b
    end
    collection do
      get 'account_activity'
    end
  end

UsersController

class UsersController < ApplicationController
  before_filter :authentication, only: [:index, :edit, :update, :destroy, :action_a, :action_b]
  #skip_before_filter :authentication, :only => [:account_activity] didn't help as well

  def account_activity
    unless current_user.nil?
      puts 'A'
      if params[:user_id] && params[:status]
        puts 'B'
        User.find(params[:user_id]).update_attributes(:active => params[:status])
        flash[:notice] = 'Activity was successfully changed.'
      end
    end
    redirect_to :back
  end
...

总是在更新 active 属性时,我会收到消息 重定向 localhost:3000/ 过滤器链因 :authentication 呈现或重定向而停止

编辑:添加了日志文件的输出:

Started GET "/users/account_activity?user_id=31&status=0" for 127.0.0.1 at 2012-09-28 00:40:10 +0200
Processing by UsersController#account_activity as HTML
  Parameters: {"user_id"=>"31", "status"=>"0"}
ApplicationController::current_user
ApplicationController::current_user_session
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."persistence_token" = '...' LIMIT 1
   (0.1ms)  BEGIN
   (0.7ms)  UPDATE "users" SET "last_request_at" = '2012-09-27 22:40:10.258152', "perishable_token" = '...', "updated_at" = '2012-09-27 22:40:10.259093' WHERE "users"."id" = 31
   (0.7ms)  COMMIT
ApplicationController::current_user_session
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "31"]]
   (0.1ms)  BEGIN
  User Exists (0.6ms)  SELECT 1 FROM "users" WHERE ("users"."email" = 'abc@def.com' AND "users"."id" != 31) LIMIT 1
   (0.5ms)  UPDATE "users" SET "active" = 0, "perishable_token" = '...', "updated_at" = '2012-09-27 22:40:10.267227' WHERE "users"."id" = 31
   (0.7ms)  COMMIT
Redirected to http://localhost:3000/users/31/edit
Completed 302 Found in 19ms (ActiveRecord: 4.5ms)


Started GET "/users/31/edit" for 127.0.0.1 at 2012-09-28 00:40:10 +0200
Processing by UsersController#edit as HTML
  Parameters: {"id"=>"31"}
ApplicationController::authentication
ApplicationController::current_user
ApplicationController::current_user_session
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."persistence_token" = '...' LIMIT 1
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 31 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :authentication rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 1.3ms)

我该如何解决这个问题?我试图在谷歌上搜索 SO,但不幸的是仍然不知道如何解决它..

【问题讨论】:

  • 您需要显示更多关于正在发生的事情,例如请求来自哪里和/或错误日志。我的猜测是,“redirect_to :back”正在向您的应用发送到 before_filter 列表中的操作之一,用于控制器顶部的 :authentication。
  • @rossta 我更新了我的 OP - 添加了日志输出。是的,我将应用程序重定向到 before_filter 列表中的操作 - 到操作 edit。但是对于edit 操作,应用程序也从update- 操作重定向,这运行良好。另外,如果我删除重定向,然后手动设置我的应用程序的home url,那么我会得到同样的错误......

标签: ruby-on-rails ruby authentication redirect action


【解决方案1】:

当您从 before_filter 重定向某处时,过滤器链停止。如果一切正常,您可以忽略此消息

【讨论】:

    【解决方案2】:

    我的一位用户遇到了这个错误。

    事实证明,用户有一个字段(我们最近添加了一个长度验证),其中的文本比允许的要多。由于验证是新的并且用户已经在那里,因此该错误一直显示。

    我在尝试使用“!”更新其属性时发现了它明确错误

    > User.last.update_attributes!(:email => "someone@domain.com") 
    

    用户负载 (0.3ms) SELECT users.* FROM users ORDER BY users.id DESC LIMIT 1 (0.1ms) BEGIN 用户存在 (0.2ms) SELECT 1 AS one FROM users WHERE (users.email = 'someone@domain.com' AND users.id != 2020) 限制 1 (0.1ms) 回滚 ActiveRecord::RecordInvalid:验证失败:摘要太长 (最多 650 个字符)

    因此更改“摘要”字段的内容解决了它。

    【讨论】:

    • +1 表示“!”-trick。对此一无所知,它为我解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-02-25
    • 2017-02-11
    • 2018-10-08
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多