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 8986709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:36:54+00:00 2026-06-15T21:36:54+00:00

I’ve worked through Ruby on Rails Tutorial by Michael Hartl, and have also implemented

  • 0

I’ve worked through Ruby on Rails Tutorial by Michael Hartl, and have also implemented a small login form included on the navbar of my application (so a user can either login from the actual login page or from the navbar login). If a user logs in from the login page, I do want them to be redireced to the root_url.

However, if the user logs in from the navbar form, I want them to stay on the same page with a “Successfully logged in!” message at the top of the page. How can I do this?

My code is as follows:

From the _header.html.erb file:

<%= form_tag sessions_path, class: "navbar-form pull-right" do %>
  <%= text_field_tag :email, nil, class: "input-small", placeholder: "Email" %>
  <%= password_field_tag :password, nil, class: "input-small", placeholder: "Password" %>
  <%= check_box_tag :remember_me, 1, params[:remember_me], title: "select to remember login" %>
  <%= submit_tag "Sign in", class: "btn btn-mini" %>
<% end %>

From the sessions_helper.rb file:

def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  session.delete(:return_to)
end

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

I have tried to leverage the redirect_back_or method from the sessions_helper.rb, but it seems to be capturing a session path as the session[:return_to] value, rather than the originating page for the login request. I would appreciate your ideas.

  • 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-06-15T21:36:55+00:00Added an answer on June 15, 2026 at 9:36 pm

    I was finally able to figure this out…

    In my sessions_helper.rb file (which is already included in the ApplicationController), I add the following code:

    def store_location_login
      session[:referral_url] = request.referer
    end
    
    def redirect_back_login_or(default)
      if URI(signin_path).path == URI(session[:referral_url]).path
      redirect_to root_url
    else
      redirect_to(session[:referral_url] || default)
    end
    
    session.delete(:referral_url)
    

    Then I changed my SessionsController create action to read:

    def create
      user = User.find_by_email(params[:email].downcase)
      store_location_login
    
      if user && user.authenticate(params[:password])
        if params[:remember_me]
          sign_in user
        else
          sign_in_temp user
        end
    
        flash[:success] = "Successfully logged in!"
        redirect_back_login_or root_url
    
      else
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    
      end
    end
    

    These changes result in the desired behavior:

    • logging in from the main login page, on successful login it redirects the user to the the root_url
    • logging in from the login form in the site navbar, it redirects back to the page of origin (unless the page of origin is the main login form)
    • this uses the same Session controller create method for each

    I’m guessing this could be refactored some, but it works.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.