Wednesday, October 20, 2010

Flex for Rails Scaffolding (cont) - the controller template

I've made a little improvement  to my development process, in such a way that the scaffolding i do for creating the web services I later use for my flex client are generated in their final form without the need to adjust them.
What i did was make some changes to the controller scaffold template.
The controller.rb template file is located at: [Ruby Home]\lib\ruby\gems\1.8\gems\rails-2.3.4\lib\rails_generator\generators\components\scaffold\templates

Backup the original (or pick it up from here).
Download the modified template


The Template should end up looking like:

class <%= controller_class_name %>Controller < ApplicationController
  # GET /<%= table_name %>
  # GET /<%= table_name %>.xml
  def index
    @<%= file_name %> = <%= class_name %>.all
   render :xml => @<%= file_name %>
  end

  # GET /<%= table_name %>/1
  # GET /<%= table_name %>/1.xml
  def show
    @<%= file_name %> = <%= class_name %>.find(params[:id])
    render :xml => @<%= file_name %>
  end

  # GET /<%= table_name %>/new
  # GET /<%= table_name %>/new.xml
  def new
   @<%= file_name %> = <%= class_name %>.new
   render :xml => @<%= file_name %>
  end

  # GET /<%= table_name %>/1/edit
  def edit
    @<%= file_name %> = <%= class_name %>.find(params[:id])
   render :xml => @<%= file_name %>
  end

  # POST /<%= table_name %>
  # POST /<%= table_name %>.xml
  def create
    @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
    if @<%= file_name %>.save
      render :xml => {:notice => '<%= class_name %> was successfully created.'}
    else
      render :xml => {:notice => @<%= file_name %>.errors}
    end
  end

  # PUT /<%= table_name %>/1
  # PUT /<%= table_name %>/1.xml
  def update
    @<%= file_name %> = <%= class_name %>.find(params[:id])

    if <%= file_name %>.update_attributes(params[:<%= file_name %>])
      render :xml => {:notice => '<%= class_name %> was successfully updated.'}
    else
      render :xml => {:notice => @<%= file_name %>.errors}
    end
  end

  # DELETE /<%= table_name %>/1
  # DELETE /<%= table_name %>/1.xml
  def destroy
    @<%= file_name %> = <%= class_name %>.find(params[:id])
    @<%= file_name %>.destroy

    render :xml => {:notice => '<%= class_name %> was successfully deleted.'}
  end
end

2 comments:

  1. Hi there, was wondering if you had any plans to update this for Rails 3? It looks great but I am having a hard time getting it working as I don't have access to a 2.3.x RAILS config...

    ReplyDelete

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.