Wednesday, September 7, 2011

JRuby Rails 3 render a file on a windows server

I want to render public/401.html if the user is not authorized to access the web site.

My environment is jruby 1.6, rails 3.0.5, and tomcat 7.0.11 on windows.

And I found the code from "JRuby cookbook"

PUBLIC_DIR = if defined?($servlet_context)
$servlet_context.getRealPath('/')
else
"#{RAILS_ROOT}" + '/public'
end

Unfortunately this code still didn't work, still got "Missing template".

The reason is windows path. PUBLIC_DIR will be "c:/tomcat/webapps/myapp" and the file is "c:/tomcat/webapps/myapp/401.html". When rails searches the view paths, it tries to add each item in view paths as a prefix of your file. One of the view paths is "c:/", then the path which rails tries to search looks like "c:/c:/tomcat/webapp/myapp/401.html". Of course, Rails cannot find the file.

$servlet_context.getRealPath('/').gsub(/^\w:/, "").gsub(/\\/, "/")

No comments:

Post a Comment