Question

I've got a comment resource. I've got a controller that handles respond_with, and for right now, it's supposed to be serving up all JSON responses (which is happening correctly). I'm using Rabl to handle my JSON/XML rendering, and I'm DRY-ing things up a little. I have the proper way I want a comment to be rendered at comments/show.rabl.

object @comment

attributes :id, :body, :a_few_more_things

When a POST call is made on /comments/ (which fires the create method on my controller), I want Rails to return the comment in the same format as the show view (above). I've got, in my create function...

def create
  # Skip some code, save it, ya-da ya-da
  respond_with(@comment, :layout => 'comments/show')
end

This isn't working; it's just returning a flat JSON implementation of the comment with all of the attributes on it. It's not using my show.rabl at comments/show.rabl. How do I get my create action to return the @comment using show.rabl as the layout?

I see this post specifies the full path and extension of the layout file; I shouldn't have to do that, should I? Am I using the wrong :symbol_option? Should it be :location?

Was it helpful?

Solution

I was able to resolve this issue by creating a Rabl template at comments/create.rabl.

object @comment

extends "comments/show"

And that's all she wrote. Rails looks for the create.rabl view, which accepts one objects and just renders the fields defined in comments/show.rabl.

Thanks to Martin Harrigan for reminding me I still had my question open!

OTHER TIPS

You can use, to stay dry :

render :show, :status => :created

In Rails4 you can specify template:

def create
  respond_with @comment, status: :created, template: 'comments/show'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top