TextMate Snippet for RESTful Controller 20
UPDATE: Fixed a couple of typos in the source code.
Here’s my TextMate snippet for generating the Golden Seven actions.
Rails 1.2 gave us the resource controller, providing a RESTful controller with an underlying model and migration.
Rails 2.0 has given us a first-class scaffold generator, which provides a RESTful controller, a model, and starter views for all actions.
But sometimes I need to roll a RESTful controller by hand. The controller generator just creates an empty controller class (I wish it would provide the RESTful skeleton
by default). So here’s my TextMate snippet that you might find helpful.
If you’ve never created a snippet before, just go to Bundles -> Bundle Editor -> Edit Snippets. Create a new bundle or add a new snippet to an existing bundle if you want.
At the bottom of the snippet dialog, specify source.ruby.rails as the Scope Selector, choose a meaningful abbreviation (I use “rest”), and paste in the code at the end
of this article.
To use the snippet, open your controller code, and then:
- type “rest” (or whatever your chosen abbreviation was) and hit the TAB key;
- the
thingscode will be automatically selected; just enter the plural of your model (“posts”, or “products”, or whatever), and then hit TAB again; - the
Thingclass name will be selected; start typing the name of your model class, and then hit TAB; - finally,
thingwill be selected; type the singular name of your model, and you’re done.
Your selections will be mirrored throughout all seven actions in all of the right places.
def index
@${1:things} = ${2:Thing}.find :all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @$1 }
end
end
def show
@${3:thing} = $2.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @$3 }
end
end
def new
@$3 = $2.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @$3 }
end
end
def edit
@$3 = $2.find(params[:id])
end
def create
@$3 = $2.new(params[:$3])
respond_to do |format|
if @$3.save
flash[:notice] = '$2 was successfully created.'
format.html { redirect_to(@$3) }
format.xml { render :xml => @$3, :status => :created, :location => @$3 }
else
format.html { render :action => "new" }
format.xml { render :xml => @$3.errors, :status => :unprocessable_entity }
end
end
end
def update
@$3 = $2.find(params[:id])
respond_to do |format|
if @$3.update_attributes(params[:$3])
flash[:notice] = '$2 was successfully updated.'
format.html { redirect_to(@$3) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @$3.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@$3 = $2.find(params[:id])
@$3.destroy
respond_to do |format|
format.html { redirect_to(admin_$1_url) }
format.xml { head :ok }
end
end





This is more useful than you may realise, since 2.0 removed the restful controller code from it's scaffold generator. As far as I can tell.
The first line in #create should be @$3 = $2.new(params[:$3]), not @$2.
Also, the first line of the destroy method should be @$3 = $2.find(params[:id])
This is great. And FYI, it works on E Text Editor on PC as well.
Thanks Joshua and Nick, I've now updated the code in the article.
ahhh, finally you've posted again.. don't go so long between posts. I like the good rails info you blog on.
This is awesome! Thanks Jeff.
This is great stuff. I do have one little improvement though. In your code you can replace
$3
with
${2/./\l$0/}
Since all of the $3's are just a $2 with the first letter lowercase.
@Brett: Excellent idea, thanks for that. I'm still trying to learn how the finer points snippet syntax.
Thanks Jeff. I have needed something like this in the past, but never thought of making a Textmate snippet for it. Great idea.
Great work! I was about to resort to writing my own when I decided to a read a blog or two instead :)
Cool set of snippets, on the other hand it left me wondering: if I need a snippet this long, something is not DRY enough at our Rails land. Then I found out a cool solution for that in the form of a Rails plugin that would render that obscene amount of code obsolete :-) Take a look:
http://www.akitaonrails.com/2008/1/25/easy-restful-rails-screencast
By virtue of human reason, our faculties are a representation of, in other words, the things in themselves.
(Natural causes (and let us suppose that this is the case) are a representation of our a priori knowledge, yet the Antinomies are what first give rise to, for example, the empirical objects in space and time) By means of analytic unity, our a priori concepts, in natural theology, would be falsified.
This could be updated once again for Rails 2.1’s introduction of named scopes.
”${2:Thing}.find :all”
to
”${2:Thing}.all”
Very very nice. Thank you.
Thanks for sharing this. Just what I was looking for. Praise you and the Google :-)
This is class, you’ve spared me lot of typing! Thanks a ton, Davide
How about an updated version for Rails 3’s respond_with?
http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/
how exactly do i use this? i put this line in my controller code: rest Kids Kid Kid
nothing happens im missing something aint i?