今天在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
分享到:
相关推荐
2. **Laravel 5 CRUD Generator** 在Laravel 5中,CRUD生成器是一个非常有用的扩展,它可以自动生成控制器、模型、视图、迁移文件,甚至是路由,为数据库操作提供基本的CRUD功能。通过简单的命令行指令,开发者可以...
6. **Scaffolding**:Rails 2.0的生成器工具(generator)提供了一种快速创建基本CRUD(创建、读取、更新、删除)操作的手段,即scaffold,它可以帮助开发者快速搭建应用的基础结构。 7. **Metal**:Rails 2.0引入...
例如,您可以生成任何以下这些基于CRUD的文件,以及更多其他文件: 游戏框架Drupal表单文件Java模型类Java JSP文件Spring Framework XML文件Ruby on Rails文件CakePHP文件任何语言SQL语句您可以想象的任何其他东西...
Rails的许多设计原则和工具都直接支持这种敏捷工作流程,如自动化测试、DHH(David Heinemeier Hansson)提出的"约定优于配置"原则以及强大的开发工具如Rails console和Rails generator。 本书的英文版可能包含以下...
使用Rails生成Golang代码或管理Go应用开发go-on-rails是Rails生成器,旨在: 帮助开发和集成一些用Golang编写的API到现有的Rails应用程序,以实现高性能使用您熟悉的Rails工具开发和管理Golang应用程序项目将不太...
【kemalyst-generator:Kemalyst的Rails命令行工具】 Kemalyst-generator是针对Kemalyst框架的一个命令行工具,它的设计灵感来源于Ruby on Rails中的scaffold生成器,旨在加速和简化Kemalyst应用的开发过程。...
标题中的“model自动生成对应crud sql语句”指的是在软件开发过程中,利用特定的工具或框架,通过定义数据模型(Model)自动生成功能齐全的CRUD(Create, Read, Update, Delete)SQL语句的技术。这种方法可以显著...
在Ruby on Rails中,开发者经常使用这样的工具来快速设置模型,这些模型对应于数据库表,并且自动实现CRUD(创建、读取、更新、删除)操作。 标签“ActiveRecord”进一步确认了这个压缩包与数据库建模和Ruby on ...
- **Rails Generator**: 如何使用Rails生成器快速创建模型、控制器和迁移。 - **Rails Console和Rails Server**: 学习如何使用命令行工具进行交互式开发和运行应用程序。 - **Form helpers**: 使用Rails内置的表单...
它基于与rails generator相同的想法。 ###先决条件: 使用 Play 框架构建的应用程序您的包/文件夹结构必须是: app/models app/controllers conf/routes ###如何使用: 安装 打开命令行并输入: npm install -g ...
6. **Rails Generator**:自动生成模型、控制器、迁移等代码,加速开发进程。 7. **Migrations**:数据库版本控制,用于跟踪和管理数据库结构的变化。 8. **RESTful**:遵循 Representational State Transfer(表述...