State of Permalinks?


#1

I’ve been working on trying to set permalinks for posts, and so far have had some success… Using the permalink param, I can modify my router to serialize accordingly:

  serialize: (model) ->
    post_id: model.get('permalink')

and on reload, my backend handles this custom ‘id’ parameter accordingly (I’m using the setup described here: https://github.com/heartsentwined/ember-auth-rails-demo/wiki/Model)

def show
  render json: Post.find_by_permalink(params[:id])
end

however I’m running into the same issue many have discussed wherein the show method successfully returns my post by permalink (and Ember uses that permalink as it’s id) and the index loads all posts with the numeric IDs, so I always wind up having a duplicate.

Since I don’t know the page id on load, im a bit confused how I can update this post with it’s actual id and hopefully remove the duplicate from the list.


#2

To avoid the duplicate I do a query find instead of finding by id.

App.UserRoute = App.Route.extend
  serialize: (model, params) ->
    if model
      { user_id: model.get('username') }
  model: (params) ->
    users = App.User.find({id: params.user_id})
    users.one 'didLoad', ->
      users.resolve(users.get('firstObject'))
    return users

#3

Looks pretty sweet… unfortunately my backend is Rails, and unless I’m missing something, the show method typically relies on a url structure instead of parameters… Would this approach require that I modify said backend to listen for an id parameter? Or is there someway around this to get Ember to throw this into the url instead of a query string?


#4

The request will go through to the index route on the Rails side so you need to look for both singular and plural forms of id, e.g.:

def index
  if params[:ids] || params[:id]
    users = User.confirmed.find(params[:ids] || params[:id])
    users = [users] if users && !users.is_a?(Array)
    respond_with users
  else
    render json: {}
  end
end