[Rails 常用插件简介]CRUD Generator 2

今天在http://agilewebdevelopment.com/plugins/crud_generator_2无意中发现的,下载下来使用了一下,感觉蛮有趣的。

CRUD Generator 2 是什么?
CRUD Generator 2 正如标题,是一个生成代码的脚手架,增删改查,和我们rails自带的resource scaffold一样,都生成基于rest的代码

一:安装
ruby script/plugin install http://crudgenerator2.googlecode.com/svn/trunk/crud_generator2 


二:生成
ruby script/generate topic


引用
      exists  app/controllers/
      exists  app/helpers/
      exists  app/views/topics
      exists  test/functional/
      exists  app/models/
      exists  test/unit/
   identical  app/controllers/topics_controller.rb
   identical  test/functional/topics_controller_test.rb
   identical  app/helpers/topics_helper.rb
   identical  app/models/topic.rb
      exists  db/migrate
      create  db/migrate/003_create_topics.rb
   identical  test/unit/topic_test.rb
   identical  test/fixtures/topics.yml
overwrite app/views/topics/index.rhtml? [Ynaqd] y
       force  app/views/topics/index.rhtml
      create  app/views/topics/new.rhtml
      create  app/views/topics/show.rhtml
      create  app/views/topics/edit.rhtml
      create  app/views/topics/_form.rhtml
      create  app/views/topics/_action_links.rhtml
      create  app/views/topics/_topic.rhtml


我们来看一下生成的controller
class TopicsController < ApplicationController

  # Do a find on the specific record before
  # show, edit, update, destroy only
  before_filter :find_topic,
    :only => [:show, :edit, :update, :destroy]

  # HTTP : GET /topics
  # SQL : SELECT
  # NAMED ROUTE : topic_url
  def index

    # We'll respond to these actions depending on the Accepts
    # header presented or by the explicit extension
    # (.html, .js, .xml, .rss, .atom, etc)
    #
    # HTML : paginated html : Accept text/html or index.html
    # JavaScript : render a JavaScript template in index.rjs
    # XML : index.xml : return all of the objects found by default
    respond_to do |format|
      format.html do
        @topics = Topic.paginate :page=>params[:page]||1
      end
      format.js
      format.xml do
        @topics = Topic.find_all
        render :xml => @topics.to_xml
      end
      #type.rss { render :action => "rss.rxml" }
      #type.atom { render :action => "atom.rxml" }
    end

  end


  # HTTP : GET /topics/new
  # SQL : No Op
  # NAMED ROUTE : new_topic_url
  def new
    @topic = Topic.new
  end

  # HTTP : POST /topics
  # SQL : INSERT
  def create

    @topic = Topic.new(params[:topic])
    @topic.save!

    respond_to do |format|
      format.html do
        flash[:notice] = "Successfuly created"
        redirect_to :action => "index"
      end
      format.js   # Renders create.rjs
      format.xml do
        headers["Location"] = topic_url(@topic)
        render(:nothing => true, :status => "201 Created")
      end
    end

  end


  # HTTP : GET /topics/1
  # SQL : SELECT
  # NAMED ROUTE : topic_url(topic_object)
  def show
    respond_to do |format|
      format.html do
        render
      end
      format.xml { render :xml => @topic.to_xml }
    end
  end


  # HTTP : GET /topics/1;edit
  # SQL : SELECT
  # NAMED ROUTE : edit_topic_url(topic_object)
  def edit
  end

  # HTTP : PUT /topics/1
  # SQL : UPDATE
  def update

    @topic.attributes = params[:topic]
    @topic.save!

    respond_to do |format|
      format.html do
        flash[:notice] = 'Successfully updated.'
        redirect_to topic_url(@topic)
      end
      format.js
    end

  end


  # HTTP : DELETE /topics/1
  # SQL : DELETE
  def destroy

    @topic.destroy

    respond_to do |format|
      format.html do
        flash[:notice] = 'Successfully destroyed.'
        redirect_to :action => "index"
      end
      format.xml { render :nothing => true }
    end

  end


  protected
    def find_topic
      begin
        @topic = Topic.find(params[:id])
      rescue
        flash.now[:warning] = 'Error, Invalid ID'
        logger.error("RescueAttemptToFindInvalidID#{params[:id]}")
      end
    end


end


修改db/migrate/**,执行
rake db:migrate

在routes.rb新增
map.resources :topics



好了,启动你的server,在浏览器中输入http://host:port/topics 就可以查看了

注:
crud2基于Edge Rails不能正常运行,附件是修改过后的代码。运行前请先安装will_paginate:http://mmm.iteye.com/blog/116931

你可能感兴趣的:(sql,xml,SQL Server,Flash,Rails)