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 }
UPDATE
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
end
Written by Emmanuel Bourgerie
Related protips
1 Response
Heh, Old good url rewrite :).
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#