Last Updated: February 25, 2016
· manudwarf

PushState "Magic Rule" in RoR

I started to work with the whole stack Backbone + RequireJS, but I encountered an issue with the routing.

The idea being to leave all the routing to Backbone, I need to pass every request to the static#home function, then Backbone routing (with pushState) does the job.

Concretely, what is the goal? Passing every html request to static#home and dissociate them from API requests (basically, JSON & XML).

This is possible by using Rails routing wildcards and constraints. You can actually copy/paste and adapt this to your routes.rb file:

# Backbone magic rule
match "/"         => "static#home",           as: "home", constraints: { format: :html }
match "*url"      => "static#home",                       constraints: { format: :html }


It seemed to work but only due to a caching issue.
Before the magic rule, don't forget to isolate the JSON api, for example:

scope "/api" do
  resources :users

1 Response
Add your response

Heh, Old good url rewrite :).

over 1 year ago ·