Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] respond_with with array of resources not working

#6413

There is a problem with respond_with arguments. Those examples behaves completely different (second one is working incorrect):

    respond_with(:admin, @post)
    respond_with([:admin, @post])

When there is passed array of resources, in this place, variable resources looks like this: [[arg1, arg2]] which is wrong. After writing a patch, one other test is falling, however I don't know what should be the correct behaviour there.
So my question is: what should look like response from api request (i.e. xml) of respond_with([:admin, @post]) - should there be only @post included or maybe :admin also?
Now it returns both arguments, however code says that this should be single resource (not resources), but now resource is mistakenly taken as passed array.

I'm submiting patches with respond_with fix and test for it - after applying them, this test is falling.

Reported by Wojciech Wnętrzak · February 11th, 2011 @ 10:01 AM

State: invalid
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Wojciech Wnętrzak
  2. Wojciech Wnętrzak
    Wojciech Wnętrzak

    I forgot to add require 'active_support/core_ext/array/wrap' for patch with fix.
    This patch should be used instead.

    February 11th, 2011 @ 10:20 AM

  3. José Valim
    José Valim
    • State changed from new to invalid
    • Assigned user set to José Valim
    • Importance changed from to Low

    Rails allows you to use respond_with([@admin, @post]) but Rails interprets it as a collection. The same as if you do: respond_with(Article.all).

    That said, if you want to say you are redirecting to a nested resource, you have to do: respond_with(@admin, @post). Therefore, [[arg1, arg2]] is not wrong and the test that fails after your change is correct.

    February 11th, 2011 @ 01:12 PM

  4. Wojciech Wnętrzak
    Wojciech Wnętrzak

    One more question:
    Let's say we have responder in create action like:

      respond_with([:admin, @post])
    
    And object @post has errors - it wont be detected and instead of 422, we get 301 - is it intentional?

    Also note about similarity of method respond_with to polymorphic_url is little misleading - there we can pass only array if we need nesting.
    Documentation

    February 11th, 2011 @ 01:37 PM

  5. José Valim
    José Valim

    Yes, it is intentional in the sense you should use respond_with(@admin, @post) instead. I agree with you about the note being misleading, I have just removed it. Thanks!

    February 11th, 2011 @ 01:51 PM