xml respond_to Default?

4 views
Skip to first unread message

hookerc...@gmail.com

unread,
Jan 31, 2008, 7:10:01 AM1/31/08
to resources_controller, ian.w...@gmail.com
Just a little question to the resources controller crew.

When using resources_controller I automatically get CRUD actions for
free, which is nice.

But what if I don't want to expose my resource as XML, since xml
response does not need a template, I can just write
users/1/comments.xml. Which is fine, but what if I dont want to expose
that resource. HTML is fine but when using XML I might expose some
private data that I did not want to?

-----------------------------------------------------------------------------------------

response_for could solve this problem but its not a great way to do
it.

response_for :index, :types => [:html] - one would assume that this
action will only now respond_to html but xml is still there, however
if I do


response_for :index, :types => [:html, :xml] - I get what I am looking
for I am forcing a template look up. i.e app error

so should the action included only have html by default and xml as an
extra option??

Thanks all

Ian White

unread,
Jan 31, 2008, 7:14:53 AM1/31/08
to hookerc...@gmail.com, resources_controller
Hey hookercookerman,

I think that :replace => true will cure your ills

response_for :index, :replace => true, :types => [:html]

Or, if you want to change the behavior globally, then make a new
Actions module for rc with different response_for blocks

Cheers,
ian
--
Argument from Design--We build web applications
Western House 239 Western Road Sheffield S10 1LE UK
Mobile: +44 (0)797 4678409 | Office: +44 (0)114 2667712
<http://www.ardes.com/> | <http://blog.ardes.com/ian>

Ian White

unread,
Jan 31, 2008, 7:20:38 AM1/31/08
to hookerc...@gmail.com, resources_controller
More on that last point.

Let's say your app does things a little different to the default rails
scaffold (with rc actions is based on) across the board. For example,
lets say you dont want xml at all. You already know that you can pass
the :actions key, but what if you just want to get rid of the xml
response once and for all?

What I would do is look at the modules in
ardes/resources_controller/actions and .../singleton_actions, chuck
these in your lib/my/actions (or whatever) making your changes

In an initializer, you can then do this:

Ardes::ResourcesController.actions = My::Actions
Ardes::ResourcesController.singleton_actions =My::SingletonActions

And the action module that RC uses by default will be set

Ian White

unread,
Jan 31, 2008, 9:15:32 AM1/31/08
to Richard Hooker, resources_controller
> I think what I was driving at, is that even thou the action can be
> overrided, should the default not have xml as standard, as there is magic
> that resources_controller
> does for you and if left you are leaving yourself open, option to include
> xml in the actions is cool but you have to ask for it?

Ah, see where you're coming from - if you don;t do anything you might
not notice some attributes leaking out in xml.

So, your suggestion is:
* remove the xml stuff from resources_controller actions
* have an option to include the standard xml stuff for REST

Any thoughts anyone?

Cheers,
Ian

Ian White

unread,
Feb 3, 2008, 6:24:55 AM2/3/08
to resources_controller
> So, your suggestion is:
> * remove the xml stuff from resources_controller actions
> * have an option to include the standard xml stuff for REST

How about I just remove the the render :xml calls in the default
actions module, (but keep the format.xml blocks).
That way a template will be required, and you'll get a 404 if you do
nothing.

Anyone who wants active_resource api (say, for show) can either

write a builder show.xml.builder template, or do

response_for :show do
format.xml { render :xml => resource }
end

This is better for following two other reasons, it forces you to think
about what attributes you want exposed via xml, and it makes the way
RC deals with responses consistent across the default html, js, and
xml mime types (look for a template)

Unless anyone has any objections, I'll make these changes

Cheers,
Ian

Ian White

unread,
Feb 3, 2008, 6:32:52 AM2/3/08
to resources_controller
> How about I just remove the the render :xml calls in the default
> actions module, (but keep the format.xml blocks).
> That way a template will be required, and you'll get a 404 if you do
> nothing.

Slight clarification:

Whenever a head, or error occurs RC will handle that as it currently
does

Whenever resource or resources is to be splat out in xml, RC will look
for show.xml.builder, or index.xml.builder
Reply all
Reply to author
Forward
0 new messages