TextMate Snippet for RESTful Controller 15

Posted by jeff Friday, December 14, 2007 02:11:00 GMT

UPDATE: Fixed a couple of typos in the source code.

Dock

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 things code will be automatically selected; just enter the plural of your model (“posts”, or “products”, or whatever), and then hit TAB again;
  • the Thing class name will be selected; start typing the name of your model class, and then hit TAB;
  • finally, thing will 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

Comments

Leave a response

  1. jason   December 14, 2007 @ 04:22 AM

    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.

  2. Joshua   December 14, 2007 @ 09:16 AM

    The first line in #create should be @$3 = $2.new(params[:$3]), not @$2.

  3. Nick Poulden   December 14, 2007 @ 11:03 AM

    Also, the first line of the destroy method should be @$3 = $2.find(params[:id])

  4. Matt   December 14, 2007 @ 11:31 AM

    This is great. And FYI, it works on E Text Editor on PC as well.

  5. Jeff   December 14, 2007 @ 02:26 PM

    Thanks Joshua and Nick, I've now updated the code in the article.

  6. josh   December 14, 2007 @ 03:23 PM

    ahhh, finally you've posted again.. don't go so long between posts. I like the good rails info you blog on.

  7. Robert Dempsey   December 14, 2007 @ 07:23 PM

    This is awesome! Thanks Jeff.

  8. Brett A. Rogers   December 15, 2007 @ 05:36 AM

    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.

  9. Jeff   December 15, 2007 @ 05:54 AM

    @Brett: Excellent idea, thanks for that. I'm still trying to learn how the finer points snippet syntax.

  10. Shane Vitarana   December 18, 2007 @ 02:40 AM

    Thanks Jeff. I have needed something like this in the past, but never thought of making a Textmate snippet for it. Great idea.

  11. Michael Air   December 21, 2007 @ 07:09 AM

    Great work! I was about to resort to writing my own when I decided to a read a blog or two instead :)

  12. AkitaOnRails   January 26, 2008 @ 02:18 PM

    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

  13. kino   May 24, 2008 @ 01:10 AM

    By virtue of human reason, our faculties are a representation of, in other words, the things in themselves.

  14. Ellroy   May 30, 2008 @ 11:18 PM

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

  15. Matthew Williams   June 09, 2008 @ 11:57 AM

    This could be updated once again for Rails 2.1’s introduction of named scopes.

    ”${2:Thing}.find :all”

    to

    ”${2:Thing}.all”

Comment