开发者

Render static files in /doc in Rails

So far I have in config/routes.rb:

match 'doc/:path' => 'doc#show'

And in app/controllers/doc_controller.rb:

class DocController < ApplicationController
  layout false

  def show
    render File.join( RAILS_ROOT, 开发者_Go百科'doc', params[:path] )
  end
end

This works find for index.html and other .html files. But it doesn't serve up .css and .js files. It also doesn't serve nested files and directories such as /doc/metrics/output/index.html

How can I get Rails to serve up all static files in /doc but without simply putting a link to them in /public (so that I can autheticate the user in the controller first)?


I would recommend not serving the files through Rails at all. Serve them through your server (Nginx, Apache). You can use the X-Accel-Redirect and the X-Sendfile headers to tell Nginx and Apache to send the static file instead. The benefit of this approach is that you can still authenticate a user before allowing them access to the file. Here's the Nginx tutorial:

http://ablogaboutcode.com/2010/11/19/serving-static-files-passenger-nginx/

Another option is to setup your routes like this:

match 'doc' => 'doc#show'

And pass your path as a parameter so you don't have to do nested URL matching in your routes, or handle special cases (.css, .js, .html, ...)

/doc?path=/path/to/my/document.css
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜