radar / guides

Guides for Ruby and Elixir and whatever else I feel like
1.52k stars 164 forks source link

Use :post_id rather than :id in post route definition #49

Closed LBRapid closed 10 years ago

LBRapid commented 10 years ago

You define the route for viewing an individual post like the following:

Blorgh.Router.map ()->
  @resource 'post', path: '/posts/:id'

This route is not incorrect in and of itself, but it won't automatically try to use the find method defined on the post model. For that to happen, it needs to be defined using :post_id rather than :id, like so:

Blorgh.Router.map ()->
  @resource 'post', path: '/posts/:post_id'

More info in the dynamic segments section of the ember routing guide at http://emberjs.com/guides/routing/defining-your-routes/

Otherwise, this is a really awesome guide :+1: Great introduction to another JS framework for me.

LBRapid commented 10 years ago

@radar

In fact, using :post_id in other places seems to make other things work without much effort as well, such as editing the post.

Changing

Blorgh.Router.map ()->
  @resource 'post', path: '/posts/:id'
  @resource 'posts.new', path:'/posts/new'
  @resource 'posts.edit', path: '/posts/:id/edit'

to

Blorgh.Router.map ()->
  @resource 'post', path: '/posts/:post_id'
  @resource 'posts.new', path:'/posts/new'
  @resource 'posts.edit', path: '/posts/:post_id/edit'

completely prevents the need to create a model method in Blorgh.PostsEditRoute, thus shortening it to:

Blorgh.PostsEditRoute = Ember.Route.extend
  actions:
    save: ->
      route = this
      this.currentModel.save().then (model) ->
        route.transitionTo('post', model)
radar commented 10 years ago

Thanks @LBRapid :) I suspected something like this, but I wasn't sure. A lot of the guides I was reading were just saying to use something like :post_id vs. :id and I thought it was more conventional than it doing something good. It's good to get confirmation of this. I really appreciate that :)