Search code examples
ruby-on-railsruby-on-rails-4routesrackmiddleman

How do I prevent my routes.rb from intercepting requests coming to /blog?


My /blog directory is just a bunch of static HTML files. That's good.

When I go to localhost/blog it works fine - it renders the index.html for my middleman generated blog. Great.

But when I click on any of the posts, it gives me a routing error:

No route matches [GET] "/blog/2015/03/11/hello_world"

I am pretty sure the reason this is happening is because of one of these rules in my routes.rb:

  get '/:friendly_id', to: 'posts#show'
  get '/rbt/:name', to: redirect {|path_params, _| "/#{path_params[:name].gsub(/^\d+\-/, '')}" } 
  get ':name',      to: 'posts#show'

I need all of these routes, but I don't want an HTML request to hit my Rack middleware unnecessarily....or worse yet, do a DB query which this error seems to suggest is happening.

How do I confine all requests to /blog/ to just resolve to my public/blog/ directory?

Edit 1

I realize the above description may not be clear. My Rails App isn't a blog, and so the posts you see referenced above, are not posts to the blog. They are posts of another kind, separately managed by the Rails app with a DB and all. I have since added a real /blog which will just be a collection of HTML articles generated by MiddleMan that will sit in my Rails /public/blog folder. The idea being that the HTML files in my /blog directory, should not hit my Rack middleware at all.


Solution

  • You can force rack to serve certain folder as static and routes-ignoring by adding config.middleware.use Rack::Static, urls: ['/blog'], root: 'public' to config/application.rb but imho it's better to setup a web server to intercept and serve /blog earlier than your app does.

    And also in your case /blog/2015/03/11/hello_world seems to be a directory name, if you add index.html to the link it should work as you expect, without changing any configuration.