respond_with with templates and http status when creating an object

48 views
Skip to first unread message

Michel Pigassou

unread,
Aug 15, 2011, 12:21:51 PM8/15/11
to rubyonra...@googlegroups.com
Hi.

When you use respond_with in a controller #create method and want to render XML the response gets the http status 201 (Created).
But if you use a view to render this XML response_with uses the status 200 (Ok), for example when you use the gem rabl.

I cannot determine if this matter is related to Rails or rabl. If it's from Rails, is there a particular reason for this behavior?

Michel Pigassou

unread,
Aug 15, 2011, 4:19:26 PM8/15/11
to rubyonra...@googlegroups.com
I was able to perform what I'm talking about in an initializer, maybe it can help seeing the big picture:

```ruby
ActionController::Responder.class_eval do
  def to_format
    if has_errors?
      api_behavior(nil) and return
    end

    controller.response.status = :created if post? && ! has_errors?

    default_render
  rescue ActionView::MissingTemplate => e
    api_behavior(e)
  end
end
```

Now I understand a lot better Rails view stack.
As for my problem, the culprit is this line in mime_responds.rb:

```
collector = Collector.new { default_render }
```

which calls the default view without options (I guess this is a functionality and not a bug since it's name is "default_render").

Michel Pigassou

unread,
Aug 17, 2011, 5:31:16 AM8/17/11
to rubyonra...@googlegroups.com
Hmm anyone?
Reply all
Reply to author
Forward
0 new messages