This project is archived and is in readonly mode.
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
-
Santiago Pastorino April 29th, 2010 @ 04:12 PM
- Milestone cleared.
- Assigned user set to José Valim
-
Lawrence Pit April 30th, 2010 @ 01:41 AM
- 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!
-
José Valim April 30th, 2010 @ 01:22 PM
- State changed from new to invalid
- Assigned user set to José Valim
-
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Low
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>