2
votes

I've scouring for a bit, but can't find an answer to this. I've got an Ember route on a Rails app, and I would like one of the model records to be displayed by default when visiting the URL (right now, a user needs to click on a specific link to retrieve the data).

For example, when visiting /albums, I would like /albums/38 to be active and visible without forcing a redirect. Is that possible?

Thanks

App.Router.map ->
@resource 'albums', path: '/', ->
    @resource 'album', path: '/:id'

App.AlbumsRoute = Ember.Route.extend
    model: -> @store.find 'album'
1

1 Answers

1
votes

Add an 'index' route for the 'albums' resource and redirect to the 'album' route with the first model. The index route will only be hit when you hit just the albums resource, and not when you hit deeper (aka not when you hit /albums/1).

App.AlbumsRoute = Ember.Route.extend
    model: -> @store.find 'album'

App.AlbumsIndexRoute = Ember.Route.extend
    model: ->
       album = this.modelFor('albums').get 'firstObject' # This can be whatever...
       this.transitionTo('album', album) if Ember.isEmpty(album) is no