Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Filter chain halted as :authentication rendered or redirected

I am still getting the "error" message in the title and don't know, how to solve it. In the 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

in the 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

and the 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
...

Always when is updated the active attribute, I will get the message Redirected localhost:3000/ Filter chain halted as :authentication rendered or redirected

EDIT: Added output from log file:

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" = '[email protected]' 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)

How can I fix this problem? I've tried to search on google here on SO, but unfortunately still don't know, how to fix it..

like image 854
user984621 Avatar asked Sep 27 '12 22:09

user984621


2 Answers

the filter chain halts when you are redirecting somewhere from before_filter. if everything working correct than you can ignore this message

like image 146
Deependra Singh Avatar answered Oct 28 '22 10:10

Deependra Singh


I got that error just with one of my users.

It turned out that the user had a field (were we recently added a lenght validation) with more text than the allowed. As the validation was new and the user was already there, that error kept showing.

I found it when trying to update its attributes using "!" to make the errors explicit

> User.last.update_attributes!(:email => "[email protected]") 

User Load (0.3ms) SELECT users.* FROM users ORDER BY users.id DESC LIMIT 1 (0.1ms) BEGIN User Exists (0.2ms) SELECT 1 AS one FROM users WHERE (users.email = '[email protected]' AND users.id != 2020) LIMIT 1 (0.1ms) ROLLBACK ActiveRecord::RecordInvalid: Validation failed: Summary is too long (maximum is 650 characters)

So changing the content of that field "Summary" solved it.

like image 42
Hugo Avatar answered Oct 28 '22 11:10

Hugo