1
votes

NOTE: This is not the same as Ember Routes and Rails Routes

I have a single-page Ember app that lives on top of a Rails stack. Disregard the need for Rails – it's needed.

My Ember application view lives at /ember_app. I would like to have normal resourceful routes map to Ember routes, like so:

GET /home     ~> /ember_app/#/home
GET /account  ~> /ember_app/#/account
GET /login    ~> /ember_app/#/login

What would be the best approach for this? One thought is:

  1. Rails routes.rb sends all requests to a single action, ember_app#index along with a parameter containing the name of the Ember route (as with redirect_route: "account"). Note home, account and login don't need anything data-wise from Rails, just want to redirect to that route in Ember.
  2. The ember_app#index action stores the route parameter into an instance variable @redirect_route
  3. In ember_app.html.erb, @redirect_route is interpolated and stored in a JavaScript variable EmberApp.redirectRoute
  4. In Ember's application_view.js (or wherever is appropriate) we use EmberApp.redirectRoute to redirect on the client side, accordingly.

Does this approach sound reasonable? We could also have Rails actions for each route, and that route just renders the index action and passes the route parameter. It seems as though, however, there could (should) be a way to map Rails routes to Ember routes programmatically. We don't want to have to duplicate them, once in the Rails routes.rb and in the Ember router.js. Any ideas there?

1

1 Answers

1
votes

I'm not sure if I understand the question correctly, but it sounds like all you need is to map everything to your ember app and then let ember do the routing.

Using a wildcard route should do the trick. Just map everything to that controller, your app will be loaded and then the Ember Router will decide what to do next.

get '(*anything)', to: 'ember_app#index'

That's the way I have set up, except that I don't do it on root, mine looks a bit more like:

get 'application_name/(*anything)', to: 'ember_app#index'

Hope this helps