#31 new
Adam Wiggins

Nested resources

Reported by Adam Wiggins | June 9th, 2008 @ 01:09 AM | in 0.4 Redux

Ok here's the more controversial feature addition. Feel free to shoot it down. :)

Nested resources:

resource 'posts' do
  get do
    # show all posts
  end

  post do
    # create new post
  end
end

resource 'posts/:id' do
  get do
    # show post params[:id]
  end

  delete do
    # destroy post params[:id]
  end

  get 'comments' do
    # show this post's comments
  end
end

The patch:

http://github.com/adamwiggins/si...

I was hesitant about this idea, since I do really like that typing "egrep '^(get|put|post|delete)' myapp.rb" returns all the full URLs. But for larger apps, having a bit more organization is really nice. And since it only adds ~20 lines of code and doesn't disrupt any existing syntax, it seemed like a net win overall.

Curious to hear feedback.

Comments and changes to this ticket

  • Adam Wiggins

    Adam Wiggins June 9th, 2008 @ 01:12 AM

    Rereading this, I probably should have mentioned that url paths are concatenated. So fetching a post's comments would look like this:

    RestClient.get 'http://localhost:4567/posts/1/comments'
    
  • Blake Mizerany

    Blake Mizerany June 9th, 2008 @ 04:01 PM

    I like that this tidies things up. Let me play with it for a bit. This is looking great.

  • Ryan Tomayko

    Ryan Tomayko June 10th, 2008 @ 05:12 AM

    I'm diggin this, too. :)

    I haven't looked at the patch yet but the syntax looks tasty.

  • Adam Wiggins

    Adam Wiggins June 23rd, 2008 @ 01:54 PM

    Branch against next:

    http://github.com/adamwiggins/si...

    This patch is still considered experimental and may not be accepted or may change significantly pending design discussions. So I'm not tagging this one with "pull"

  • Dan Kubb

    Dan Kubb August 14th, 2008 @ 07:24 PM

    • → Tag changed from “” to “feature patch”
    1. I love this idea. I had one suggestion though, but feel free to tell me to create a new ticket if you think this should be considered separately from this feature.

    In your second resource block, where you have the :id parameter in the URL, what if the method blocks yield'ed with the parameters extracted from the URL? For example:

    @@@ruby resource 'posts/:id' do get do |id|

    # show post with id
    

    end

    delete do |id|

    # destroy post with id
    

    end

    get 'comments' do |id|

    # show comments of post with id
    

    end end

    
    
  • Dan Kubb

    Dan Kubb August 14th, 2008 @ 07:26 PM

    Wow, I fubared the formatting on that, and I don't have access to edit it after posting. Here's a gist showing what I mean: http://gist.github.com/5524

  • Ryan Tomayko

    Ryan Tomayko September 7th, 2008 @ 06:26 PM

    • → Milestone changed from “0.3.0 Sammy” to “0.4 Redux”

    I'd like to move this to 0.4 and the redux branch if there's no objections. Feels like the new code layout would be more accepting of such a change.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tags