Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no respond_to block in edit action (generated with scaffold)?

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).

like image 465
Christoph Schiessl Avatar asked Sep 22 '08 14:09

Christoph Schiessl


3 Answers

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.

like image 120
Pete Avatar answered Oct 18 '22 04:10

Pete


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.

like image 41
Ryan McGeary Avatar answered Oct 18 '22 06:10

Ryan McGeary


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.

like image 37
Andrew Avatar answered Oct 18 '22 05:10

Andrew