Jruby sinatra app routing issue within the post route handler ( warbler generated war file sub-uri deployment )

StackOverflow https://stackoverflow.com/questions/21922053

  •  14-10-2022
  •  | 
  •  

Question

I implemented a small jruby sinatra app and if i run it directly on WEBrick locally all the routings work perfectly. However when I deploy the war file (i use warbler) to a server instance (like "example.com/myapp" or "localhost:8080/myapp") I have routing issues within the post requests.

For example:

get '/login' do
   slim :login
end


post  '/login' do
  session.clear
  login_correct? = check_password (params[:user], params[:pass])

  if(login_correct?)
    session[:user] = params[:user]
    redirect to('/')
  else
    redirect to('/login')
  end
end


get '/redirect' do
  redirect to('/login')
end

Here the 3rd route handler (get '/redirect' do ..) redirects to localhost:8080/myapp/login properly with status code 303, however 2nd route handler redirects to localhost:8080/login with status code 404.

What should i do so that redirections in post route handler works properly when i deploy the app?

Thanks a lot!

UPDATE on Solution: After checking the code again and again I realized that the problem was me using form action = '/login' in slim:login instead of form action= "#{url('/login')}". So it wasn't even handled by the post route handler since the post request was sent to localhost:8080/login but I thought it was route handler who is redirecting it to there..

Was it helpful?

Solution

try setting set :prefixed_redirects, true with Sinatra

(it should than use rack.env['SCRIPT_NAME'] with redirects)

UPDATE: even without the set :prefixed_redirects, true (default is false) works perfectly fine under Trinidad (which should behave the same as Warbler since bot use JRuby-Rack) ... request.env['SCRIPT_NAME'] is set to the /context_path correctly.

I would try updating (if they're not the latest) JRuby-Rack/Warbler as well as Sinatra as a last resort, otherwise this probably needs a detailed look at what's going on (esp. if SCRIPT_NAME is set correctly).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top