views:

289

answers:

3

Does anyone know why there is no respond_to block for generated edit actions? Every other action in typical scaffold controllers has a respond_to block in order to output html and xml formats. Why is the edit action an exception?

I'm using the latest version of Ruby on Rails (2.1.1).

+1  A: 

Because the edit action will only be called from HTML There is no need for the edit form to be returned in an XML context. Using REST, you simply make a put call directly to update with the relevant information.

Andrew
+11  A: 

Rails handles the 99% case: It's fairly unlikely you'd ever need to do any XML or JSON translations in your Edit action, because non-visually, the Edit action is pretty much just like the Show action. Nonvisual clients that want to update a model in your application can call the controller this way

GET /my_models/[:id].xml (Show)

Then, the client app can make any transformations or edits and post (or put) the results to

PUT /my_models/[:id].xml (Update)

When you call this, you usually are doing it to get an editable form of the Show action:

GET /my_models/[:id]/edit

And it is intended for human use. 99% of the time, that is. Since it's unusual to transform the data in the Edit action, Rails assumes you aren't going to, and DRYs up your code by leaving respond_to out of the scaffold.

Pete
I understand that. However, there is a respond_to block for new actions. Wouldn't the same argument apply to the new actions as well?
Christoph Schiessl
The "new" action gives you an object with default fields filled in. You'll use them when you subsequently post to the "create" action, whether from a nonvisual client or an HTML screen.
Pete
And......as Ryan points out in his answer: The New action returns an XML template that you can use to fill in your model from a nonvisual client. Good call, Ryan.
Pete
+2  A: 

Somewhat related. Some may wonder why the rails scaffolding for the new action still has a respond_to block; whereas the edit action does not. This is because a request to something like:

GET /my_models/new.xml

...gives back an XML template that can be used to create a new model.

Ryan McGeary