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

  • SEARCH
  • Home
  • 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 7678913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:45:19+00:00 2026-05-31T17:45:19+00:00

In my application, I store the user’s ID in session[]. At the beginning of

  • 0

In my application, I store the user’s ID in session[]. At the beginning of every controller action, I’m calling a method defined in the ApplicationController called current_user:

  def current_user
    @current_user ||= session[:current_user_id] && 
    User.find_by_id(session[:current_user_id])
  end

At the beginning of my controllers’ methods, I have the following:

  @current_user = current_user
  if @current_user == nil     
    redirect_to :home         
    return                    
  end                         

This is obviously repetitive code and should be a method somewhere. I read the answer for this question, and tried putting my method into a parent class that my controller classes now descend from, however it seems like I can’t redirect from that method now.

In my parent class, I have:

  def verify_user
    user = current_user
    if user == nil
      redirect_to "/"
      return
    end
    return user
  end

And now I’ve changed my controller methods to this:

  @current_user = verify_user

This doesn’t work, and I think I know why. For one, I can’t simply call return in my verify_user method, as that obviously will just return to the controller. The redirect doesn’t seem to have any affect, probably because format.html is being called after the redirect call, which was the reason for the return in the original code.

So, what am I doing wrong here, and what suggestion do you have to solve it? Is this the wrong approach? My main goal is to keep the entire “check if user is logged in otherwise redirect” to one line of code per controller method.

  • 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-31T17:45:21+00:00Added an answer on May 31, 2026 at 5:45 pm

    Add the following logic to the ApplicationController class:

    class ApplicationController < ActionController::Base
    
      def current_user
        ...
      end
    
      def logged_in?
        current_user.present?
      end
    
      def require_user
        return true if logged_in?
        render_error_message("You must be logged in to access this page", 
          new_user_session_url)
        return false
      end
    
      def render_message message
        respond_to do |format|
          format.html {
            if request.xhr? 
              render(:text => message, :status => :unprocessable_entity)
            else
              redirect_to(root_url, :notice => message)        
            end
          }
          format.json { render :json => message, :status => :unprocessable_entity }
          format.xml { render :xml => message, :status => :unprocessable_entity }
        end
      end
    
    end
    

    Now add a before_filter to your controller:

    class OrdersController < ApplicationController
    
      before_filter :require_user
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Where do you store user-specific and machine-specific runtime configuration data for J2SE application? (For
My application makes use of a SQLite database to store the user's inputs. The
I have created an application that uses settings.settings to store some user specific settings
I need a way to store application-level data (i.e. cross user sessions) in ASP.NET.
I have an AppEngine application that uses the blobstore to store user-provided image data.
Which is the best way to store application and user settings of an application
I am writing a .net(C#) windows application to store user passwords in it, like
My application uses MSXML version 1 (MSXML.DOMDocument) to store user documents in XML format.
I need to store user preferences on a per page basis in my application.
My application use application settings to store user's settings, the settings file name is

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.