NetBeans code template for RESTful controller

I’m using NetBeans as my IDE for my Ruby related (and not only) work. Since I’m writing RESTful application I do create a lot of Restful controllers. And I was tired creating Golden Seven actions again and again. So I finally sat and learned a little about NetBeans code templates.

So here it comes code for create RESTful controller:


  def index
    @${1 default="PLURAL"} = ${2 default="MODEL"}.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @${1 editable="false"} }
      format.json  { render :json => @${1 editable="false"} }
    end
  end


  def show
    @${3 default="SINGULAR"} = ${2 editable="false"}.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @${3 editable="false"} }
      format.json  { render :json => @${3 editable="false"} }
    end
  end

  def new
    @${3 editable="false"} = ${2 editable="false"}.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @${3 editable="false"} }
      format.json  { render :json => @${3 editable="false"} }
    end
  end

  def edit
    @${3 editable="false"} = ${2 editable="false"}.find(params[:id])
  end

  def create
    @${3 editable="false"} = ${2 editable="false"}.new(params[:${2 editable="false"}])

    respond_to do |format|
      if @${3 editable="false"}.save
        flash[:notice] = '${2 editable="false"} was successfully created.'
        format.html { redirect_to(@${3 editable="false"}) }
        format.xml  { render :xml => @${3 editable="false"}, :status => :created, :location => @${3 editable="false"} }
        format.json { render :json => @${3 editable="false"}, :status => :created, :location => @${3 editable="false"} }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @${3 editable="false"}.errors, :status => :unprocessable_entity }
        format.json { render :json => @${3 editable="false"}.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @${3 editable="false"} = ${2 editable="false"}.find(params[:id])

    respond_to do |format|
      if @${3 editable="false"}.update_attributes(params[:${3 editable="false"}])
        flash[:notice] = '${2 editable="false"} was successfully updated.'
        format.html { redirect_to(@${3 editable="false"}) }
        format.xml  { head :ok }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :xml => @${3 editable="false"}.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @${3 editable="false"} = ${2 editable="false"}.find(params[:id])
    @${3 editable="false"}.destroy

    respond_to do |format|
      format.html { redirect_to(${3 editable="false"}s_url) }
      format.xml  { head :ok }
      format.json  { head :ok }
    end
  end

How to use it?

Copy following code, go to Tools / Options / Editor / Code Templates, select Ruby language, click New and create new code template. I have used abbreviation rc.

Now I can generate empty controller, go to it’s body, type rc and press TAB – now just type three names (TAB after each one). First is plural for variable names, second is model name and third is singular variable name. If we would generate PostsController we would go to empty body of PostsController class and type rc, TAB, posts, TAB, Post, TAB, post. Done.

I was inspired by this TextMate snippet To create this code template

I’m using developer build of NetBeans, but this code template should work in older NB too.

Join the Conversation

4 Comments

  1. @ciukes
    Because I don’t need model (API wrapping existing code) and it is easier to customize – I guess so, since I haven’t tried to alter generators output.

    @Pierre
    resource_controller is good option if You don’t need to customize code in controller. In my case I need to customize (wrapping existing not so fat models) a lot.

    Most of those can be done with resource_controller but code become more cryptic – especially if I do not write for ‘myself’ – this code will be probably maintained by someone else. So more ‘standard’ solutions I use my customer will be more happy.

  2. I totally agree with your argument. Sometimes too much Rails magic can obfuscate and get in the way of what you are trying to achieve, especially when your solution needs a lot of customization.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.