Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:58:59+00:00 2026-05-20T04:58:59+00:00

I have a before_filter in my application controller to keep a user’s session alive

  • 0

I have a before_filter in my application controller to keep a user’s session alive (and log them out if a time out has been reached). This should be called on every action except /sessions/new and /sessions/destroy which are routed as /login and /logout.

The relevant parts of my application controller look like this;

class ApplicationController < ActionController::Base
  before_filter :update_activity_time, :except => [:login, :logout]

  private

  def update_activity_time
    if current_user
      time_out = current_user.setting.remember_me ? 20160 : current_user.setting.user_timeout
      from_now = time_out.minutes.from_now
    end
    if session[:expires_at].blank?
      session[:expires_at] = from_now
    else
      time_left = (session[:expires_at].utc - Time.now.utc).to_i
      if time_left <= 0
        session_expiry
      else
        session[:expires_at] = from_now
      end
    end
  end

  def session_expiry
    reset_session
    flash[:notice] = 'Your session has expired. Please log back in.'
    unless request.xhr?
      session[:return_to] = request.request_uri
      redirect_to login_url
    else
      session[:return_to] = request.referer
      render :js => "window.location.replace(\"#{login_url}\")"
    end
  end

end

and my routes.rb contains the following;

map.login "login", :controller => "sessions", :action => "new"

map.logout "logout", :controller => "sessions", :action => "destroy"

The before_filter is being called when /login or /logout are being visited. This isn’t a show-stopper but it does cause a few odd behaviours (e.g. when logging out from a page that has timed out).

Any ideas what I’m doing wrong? I’m using Rails 2.3.10.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:59:00+00:00Added an answer on May 20, 2026 at 4:59 am

    The :except option takes action names, not url parts. Here’s what you should do instead:

    class ApplicationController < ActionController::Base
      before_filter :update_activity_time
      ...
    end
    

    Then, in sessions_controller.rb:

    class SessionsController < ApplicationController
      skip_before_filter :update_activity_time, :only => [:new, :destroy]
      ...
    end
    

    You don’t want to put the :except in ApplicationController because, if you did, the new and destroy actions for every one of your app’s controllers wouldn’t update the activity time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say in my application controller, I have a before_filter that gets fired, loads some
I have been debugging this the entire day. I have two models in my
Ok, I have a Rails 3 application and I am using CouchDB as my
While writing functional tests for a controller, I came across a scenario where I
HI , I am very new to rails application . I am trying to
I have separate models for Devise users and admins. I am also using Basecamp
I have a Rails 3 app running on Heroku and I also have a
Rails somehow mixes my locales an I have absolutely no clue why. Most of
I understand that we're supposed to avoid putting logic in the controller. So what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.