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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:36:19+00:00 2026-06-12T09:36:19+00:00

In my controller I have the following post /buy_item do redirect ‘/login’ unless session[:name]

  • 0

In my controller I have the following

post "/buy_item" do
    redirect '/login' unless session[:name]

    @owner = Market::User.user_by_name(params[:owner])
    @item = @owner.item_by_id(params[:item].to_i)

    @current_user = Market::User.user_by_name(session[:name])
    if @current_user.buy_item?(@item)
      @current_user.buy_item(@item)
    else
      redirect '/error'
    end
  end

when I’m forcing an “else” I get correctly redirected to /error, but directly after that I get redirected to another page (/?loggedin=true). redirect "/?loggedin=true" is the last line of the post "/login" method. So, it seems that it is calling POST /login somehow..

The route for /error looks like this:

  get "/error" do
    redirect '/login' unless session[:name]

    template = ERB.new File.new($VIEWS_FOLDER + "/error.erb").read, nil, "%"
    template.result(binding)
  end

Nothing in /error.erb is redirecting, when I call localhost:4567/error directly it doesn’t get redirected.

Here’s the log:

127.0.0.1 – – [03/Oct/2012 17:15:03] “POST /login HTTP/1.1” 303 – 0.0012

localhost – – [03/Oct/2012:17:15:03 CEST] “POST /login HTTP/1.1” 303 0

localhost:4567/login -> /login

127.0.0.1 – – [03/Oct/2012 17:15:03] “GET /?loggedin=true HTTP/1.1” 200 3916 0.0055

localhost – – [03/Oct/2012:17:15:03 CEST] “GET /?loggedin=true
HTTP/1.1” 200 3916

localhost:4567/login -> /?loggedin=true

127.0.0.1 – – [03/Oct/2012 17:15:05] “POST /buy_item HTTP/1.1” 303 – 0.0030

localhost – – [03/Oct/2012:17:15:05 CEST] “POST /buy_item HTTP/1.1”
303 0

localhost:4567/?loggedin=true -> /buy_item

127.0.0.1 – – [03/Oct/2012 17:15:05] “GET /error HTTP/1.1” 200 1609 0.0039

localhost – – [03/Oct/2012:17:15:05 CEST] “GET /error HTTP/1.1” 200
1609

localhost:4567/?loggedin=true -> /error

127.0.0.1 – – [03/Oct/2012 17:15:05] “GET /?loggedin=true HTTP/1.1” 200 3916 0.0063

localhost – – [03/Oct/2012:17:15:05 CEST] “GET /?loggedin=true
HTTP/1.1” 200 3916

localhost:4567/login -> /?loggedin=true

  • 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-12T09:36:20+00:00Added an answer on June 12, 2026 at 9:36 am

    You have two redirects inside your route '/buy_item'; and in '/error' you do not return after the redirect. redirect does something to your HTTP header and it is good practice to return after the call, i.e. in both routes /buy_item and /error:

    -redirect '/login' unless session[:name]
    +unless session[:name]
    +  redirect '/login'
    +  return nil
    +end
    

    I have worked on a large Sinatra app and there were a lot of problems with redirect, the above patch should help. Moreover I recommend you to take a look at the HTTP RFC and make yourself familiar with the Post-Redirect-Get scheme. To to redirects with custom status code, you can do redirect '/foo', 303.

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

Sidebar

Related Questions

Using the ASP.NET Web-Api, I have the following POST setup in my controller. When
I have a jQuery post call like the following: $.post('/Controller/_PartialAction', { 'param1': data },
I have a controller method with the following signature: [AcceptVerbs(HttpVerbs.Post)] public ActionResult UpdateValues(int id,
I have a simple controller test, containing a.o. the following code: context POST :create
I have following spec to test controller method: context #create do it should redirect
In Spring MVC I have the following controller: @RequestMapping(value=adminUsers, method = RequestMethod.POST) public ModelAndView
I have the following code in my controller after the user posts the form
I have the following urlmappings: /loader/ { action = [POST:savep] controller = 'loader' }
I have the following update code in the ASP.NET MVC controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult
i'm trying to have the following structure get /something/id/apply => controller 1 post /something/id/apply

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.