Skip to content

war application links and the context path #110

bbergstrom opened this Issue Sep 11, 2012 · 6 comments

4 participants


I am building a padrino/sinatra application, which creates multiple mounted apps, the main(/) and admin(/admin). This works great when running padrino directly, but has issues when I put it in a war using warbler because it gets deployed to tomcat with a context path "/context_path". The links in the admin part of the application are absolute paths, which all 404. I could put apache in front of it to make it run as the root of a virtual host, but I was wondering if there was a good way to make the link generation prepend the context path in some way?


Anyone have any thoughts on this issue?

mjwall commented Sep 28, 2012

I haven't used Padrino, but I have created a helper like the following in Sinatra.

module Helpers

    def context_path
      @context_path ||= check_for_context

    def check_for_context
      if $servlet_context
        return $servlet_context.getContextPath
        return ""

This should only call check_for_context once and cache it. Then use context_path in your views, like this in HAML

%link{:href => "#{@context_path}/css/main.css", :rel => "stylesheet", :type => "text/css"}
JRuby Team member

You might be better off just deploying your war as ROOT.war in Tomcat to get around this issue, if you don't want to re-codd paths in your application.


Sometimes root isn't an option.
jruby-rack seems to be coded to handle this, but maybe Sinatra/Padrio aren't setting this param?

mjwall commented Oct 3, 2012

That is the conclusion that I came to as well, namely sinatra doesn't handle the context path. The code I provided worked for me, but I had to prepend the context_path to my links. I just noticed my code is calling the variable, not the method, so try this in your views, again shown with haml syntax.

%link{:href => "#{context_path}/css/main.css", :rel => "stylesheet", :type => "text/css"}
r4um commented Oct 11, 2013

Works correctly when using the sinatra url helper.

%link{:href => "#{url('/css/main.css')}", :rel => "stylesheet", :type => "text/css"}

Also use redirect to('/url') instead of redirect '/url when redirecting from sinatra.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.