Ruby on Rails Wednesday, December 28, 2011

I've just finished a toy project that I call "righteously ubiquitous
javascript":

https://github.com/rdpoor/righteous_ujs

and the experience has convinced me that Rails controllers need a
#delete method that parallels the contracts of the #new and #edit
methods. To explain: The three "verbs" that cause a change to model
state are:

CREATE a model instance
MODIFY a model instance
DELETE a model instance

For each verb, the controller needs to implement TWO methods. The first
method fields the request from the user to initiate the action, to which
the controller responds by rendering a GUI element that allows the user
to complete the command. The second method is the result of submitting
the GUI element.

That's actually the contract of #new and #edit:

#new => render a GUI element that calls #create when submitted
#edit => render a GUI element that calls #update when submitted

The problem is with DELETE (aka #destroy). It doesn't follow this
pattern, but it should: the controller lacks a method that renders a GUI
element. It OUGHT to be:

#delete => render a GUI element that calls #destroy when submitted

Absent a #delete method, the Rails ends up relying on Javascript to
generate the GUI element that calls #destroy, which is Not Nice in a
system that claims to implement ubiquitous javascript.

This could easily be fixed by adding a #delete method to
ApplicationController which parallels #new and #edit. It would avoid
the issue about requiring Javascript, and as a side effect, would make
it really easy for newcomers to understand the relationship between:

#new => #create
#edit => #update
#delete => #destroy

Make sense? Is DHH listening? :)

Peace out.

- ff

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment