Vim Snippets for Ruby on Rails

Last weekend, I joined Webgeek Devcup but did not present anything as I was not ready. It was a really fun experience to join a hackathon for the first time. Great to have met some people there.

During the event, I started working on some VIM snippets though.

For those who use Textmate the Rails TM bundle, there’s a snippet called “resources” that allows you create a nice controller with CRUD.

It renders something similar to the code below but this is a lot better:

class PostController < ApplicationController

  respond_to :html, :json

  before_filter :find_post, :only => [:show, :edit, :update, :destroy]

  def index
    @posts = Post.order('created_at DESC')
  end

  def show
  end

  def new
    @post = Post.new
  end

  def edit
  end

  def create
    @post = Post.new(params[:post])
    if @post.save
      flash[:notice] = "The post was successfully created."
      respond_with @post
    else
      render action: "new"
    end
   end

  def update
    if @post.update_attributes(params[:post])
      flash[:notice] = "The post was successfully updated."
      respond_with @post
    else
      render action: "edit"
    end
  end

  def destroy
    if @post.destroy
      flash[:notice] = "The post was successfully deleted."
      respond_with @post
    else
      redirect_to posts_path
    end
  end

  private

  def find_post
    @post = Post.find(params[:id])
  end

end

I worked on “resources” snippet which renders that.

If you use Vundle, just include the following if you want to have snippets:

Bundle "MarcWeber/vim-addon-mw-utils"
Bundle "tomtom/tlib_vim"
Bundle 'bridgeutopia/snipmate-snippets'
Bundle "garbas/vim-snipmate"

And :BundleInstall

I’ll add more snippets as needed. So far, that’s what I like about VIM. The ability to easily create snippets. Glad I got this VIM book for free.

Vim snippets are really helpful but I think speed in typing is the least concern in programming.