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.

respond_with accepting options to pass on to to_xml and to_json

#4499

Using rails 3 beta 3. In Rails 2 you could do:

   format.xml  { render :xml => @person.to_xml(:include => @company) }

In rails 3 it's preferred to simply do:

   respond_to :json, :xml
   def show
      respond_with(@person)
   end

However, how would you include the @company in the output?

Unless I'm missing something, it seems it's not possible at the moment to pass extra options to the formatter. I'm referring to all the options of ActiveModel::Serializers::JSON encode_json method and ActiveRecord::Serialization to_xml.

I'm thinking about e.g.:

  respond_with(@person, :include => @company)

Not sure though if this would be a good idea, as

1) the options hash seems to be meant for Responder

2) it would mean the various renderers/formatters should share the same set of option keys; which in the case of to_json and to_xml isn't that irrational, but it may not jive with other formatters.

In any case, in practice it turns out that respond_with has limited value for me, as often I need to include some extra data and/or exclude attributes. I'm happy to provide a patch, but I'd appreciate some guidance first: any suggestions if and how this situation could be improved?

Reported by Lawrence Pit · April 29th, 2010 @ 02:10 PM

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

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user set to José Valim

    April 29th, 2010 @ 04:12 PM

  2. José Valim
    José Valim

    I think it already works on Rails master. Have you tried?

    April 29th, 2010 @ 04:29 PM

  3. Lawrence Pit
    Lawrence Pit
    • Assigned user cleared.

    I hadn't, but just tried. So the options are now passed on, yes. Unfortunately, the to_xml method contains a bug, for which I opened a separate ticket: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...

    This ticket can be closed. Thanks!

    April 30th, 2010 @ 01:41 AM

  4. José Valim
    José Valim
    • State changed from new to invalid
    • Assigned user set to José Valim

    April 30th, 2010 @ 01:22 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM