This project is archived and is in readonly mode.

#4499 ✓invalid
Lawrence Pit

respond_with accepting options to pass on to to_xml and to_json

Reported by Lawrence Pit | April 29th, 2010 @ 02:10 PM | in 3.0.2

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?

Comments and changes to this ticket

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages