Search code examples
ruby-on-railsrubyrails-routing

Rails catch all route prevents access to public folder


... which makes sense. I'm using the catch all route for page routing in a custom CMS. However I would still like to access user uploaded assets in my public folder. My route is as follows:

match "(*url)" => 'pages#show'

And my controller does this:

def show
    @page = Page.where(:url => ['/', params[:url]].join).first
     begin
        render :template => "templates/" + @page.template.slug
     rescue
        render "public/404.html"
     end
end

Now I know I could probably parse the url in my controller but I was wondering if there is a better rails way - for example, can I add an exception to the catch all route?

Thoughts?


Solution

  • Generally static files will take precedence over Rails generated content if your server is configured correctly.

    Be careful not to confuse the path relative to the application, such as /public/images/test.png with the path relative to the web root, which would be /images/test.png. This conflicting definition of "path" has confused many developers.