- 浏览: 2678430 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
80后的童年2:
深入浅出MongoDB应用实战开发网盘地址:https://p ...
MongoDB入门教程 -
shliujing:
楼主在不是精通java和php的前提下,请不要妄下结论。
PHP、CakePHP哪凉快哪呆着去 -
安静听歌:
希望可以一给一点点注释
MySQL存储过程之代码块、条件控制、迭代 -
qq287767957:
PHP是全宇宙最强的语言!
PHP、CakePHP哪凉快哪呆着去 -
rryymmoK:
深入浅出MongoDB应用实战开发百度网盘下载:链接:http ...
MongoDB入门教程
对于如下代码:
这将生成如下代码:
原来scaffolding是一个Rails的插件,源码如下:
现在的scaffolding源码在$RUBY_HOME\lib\ruby\gems\1.8\gems\actionpack-1.13.3\lib\action_controller\scaffolding.rb:
其中module_eval的方式很不错
class WeblogController < ActionController::Base scaffold :entry end
这将生成如下代码:
class WeblogController < ActionController::Base verify :method => :post, \:only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def index list end def list @entries = Entry.find(:all) render_scaffold "list" end def show @entry = Entry.find(params[:id]) render_scaffold end def destroy Entry.find(params[:id]).destroy redirect_to :action => "list" end def new @entry = Entry.new render_scaffold end def create @entry = Entry.new(params[:entry]) if @entry.save flash[:notice] = "Entry was successfully created" redirect_to :action => "list" else render_scaffold('new') end end def edit @entry = Entry.find(params[:id]) render_scaffold end def update @entry = Entry.find(params[:id]) @entry.attributes = params[:entry] if @entry.save flash[:notice] = "Entry was successfully updated" redirect_to :action => "show", :id => @entry else render_scaffold('edit') end end end
原来scaffolding是一个Rails的插件,源码如下:
module Scaffolding # :nodoc: def self.included(base) base.extend(ClassMethods) end # Scaffolding is a way to quickly put an Active Record class online by providing a series of standardized actions # for listing, showing, creating, updating, and destroying objects of the class. These standardized actions come # with both controller logic and default templates that through introspection already know which fields to display # and which input types to use. Example: # # class WeblogController < ActionController::Base # scaffold :entry # end # # This tiny piece of code will add all of the following methods to the controller: # # class WeblogController < ActionController::Base # verify :method => :post, \:only => [ :destroy, :create, :update ], # :redirect_to => { :action => :list } # # def index # list # end # # def list # @entries = Entry.find(:all) # render_scaffold "list" # end # # def show # @entry = Entry.find(params[:id]) # render_scaffold # end # # def destroy # Entry.find(params[:id]).destroy # redirect_to :action => "list" # end # # def new # @entry = Entry.new # render_scaffold # end # # def create # @entry = Entry.new(params[:entry]) # if @entry.save # flash[:notice] = "Entry was successfully created" # redirect_to :action => "list" # else # render_scaffold('new') # end # end # # def edit # @entry = Entry.find(params[:id]) # render_scaffold # end # # def update # @entry = Entry.find(params[:id]) # @entry.attributes = params[:entry] # # if @entry.save # flash[:notice] = "Entry was successfully updated" # redirect_to :action => "show", :id => @entry # else # render_scaffold('edit') # end # end # end # # The <tt>render_scaffold</tt> method will first check to see if you've made your own template (like "weblog/show.erb" for # the show action) and if not, then render the generic template for that action. This gives you the possibility of using the # scaffold while you're building your specific application. Start out with a totally generic setup, then replace one template # and one action at a time while relying on the rest of the scaffolded templates and actions. module ClassMethods # Adds a swath of generic CRUD actions to the controller. The +model_id+ is automatically converted into a class name unless # one is specifically provide through <tt>options[:class_name]</tt>. So <tt>scaffold :post</tt> would use Post as the class # and @post/@posts for the instance variables. # # It's possible to use more than one scaffold in a single controller by specifying <tt>options[:suffix] = true</tt>. This will # make <tt>scaffold :post, :suffix => true</tt> use method names like list_post, show_post, and create_post # instead of just list, show, and post. If suffix is used, then no index method is added. def scaffold(model_id, options = {}) options.assert_valid_keys(:class_name, :suffix) singular_name = model_id.to_s class_name = options[:class_name] || singular_name.camelize plural_name = singular_name.pluralize suffix = options[:suffix] ? "_#{singular_name}" : "" unless options[:suffix] module_eval <<-"end_eval", __FILE__, __LINE__ def index list end end_eval end module_eval <<-"end_eval", __FILE__, __LINE__ verify :method => :post, \:only => [ :destroy#{suffix}, :create#{suffix}, :update#{suffix} ], :redirect_to => { :action => :list#{suffix} } def list#{suffix} @#{singular_name}_pages, @#{plural_name} = paginate :#{plural_name}, :per_page => 10 render#{suffix}_scaffold "list#{suffix}" end def show#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) render#{suffix}_scaffold end def destroy#{suffix} #{class_name}.find(params[:id]).destroy redirect_to :action => "list#{suffix}" end def new#{suffix} @#{singular_name} = #{class_name}.new render#{suffix}_scaffold end def create#{suffix} @#{singular_name} = #{class_name}.new(params[:#{singular_name}]) if @#{singular_name}.save flash[:notice] = "#{class_name} was successfully created" redirect_to :action => "list#{suffix}" else render#{suffix}_scaffold('new') end end def edit#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) render#{suffix}_scaffold end def update#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) @#{singular_name}.attributes = params[:#{singular_name}] if @#{singular_name}.save flash[:notice] = "#{class_name} was successfully updated" redirect_to :action => "show#{suffix}", :id => @#{singular_name} else render#{suffix}_scaffold('edit') end end private def render#{suffix}_scaffold(action=nil) action ||= caller_method_name(caller) # logger.info ("testing template:" + "\#{self.class.controller_path}/\#{action}") if logger if template_exists?("\#{self.class.controller_path}/\#{action}") render :action => action else @scaffold_class = #{class_name} @scaffold_singular_name, @scaffold_plural_name = "#{singular_name}", "#{plural_name}" @scaffold_suffix = "#{suffix}" add_instance_variables_to_assigns @template.instance_variable_set("@content_for_layout", @template.render_file(scaffold_path(action.sub(/#{suffix}$/, "")), false)) if !active_layout.nil? render :file => active_layout, :use_full_path => true else render :file => scaffold_path('layout') end end end def scaffold_path(template_name) File.dirname(__FILE__) + "/templates/" + template_name + ".erb" end def caller_method_name(caller) caller.first.scan(/`(.*)'/).first.first # ' ruby-mode end end_eval end end end
现在的scaffolding源码在$RUBY_HOME\lib\ruby\gems\1.8\gems\actionpack-1.13.3\lib\action_controller\scaffolding.rb:
module ActionController module Scaffolding # :nodoc: def self.included(base) base.extend(ClassMethods) end # Scaffolding is a way to quickly put an Active Record class online by providing a series of standardized actions # for listing, showing, creating, updating, and destroying objects of the class. These standardized actions come # with both controller logic and default templates that through introspection already know which fields to display # and which input types to use. Example: # # class WeblogController < ActionController::Base # scaffold :entry # end # # This tiny piece of code will add all of the following methods to the controller: # # class WeblogController < ActionController::Base # verify :method => :post, \:only => [ :destroy, :create, :update ], # :redirect_to => { :action => :list } # # def index # list # end # # def list # @entries = Entry.find(:all) # render_scaffold "list" # end # # def show # @entry = Entry.find(params[:id]) # render_scaffold # end # # def destroy # Entry.find(params[:id]).destroy # redirect_to :action => "list" # end # # def new # @entry = Entry.new # render_scaffold # end # # def create # @entry = Entry.new(params[:entry]) # if @entry.save # flash[:notice] = "Entry was successfully created" # redirect_to :action => "list" # else # render_scaffold('new') # end # end # # def edit # @entry = Entry.find(params[:id]) # render_scaffold # end # # def update # @entry = Entry.find(params[:id]) # @entry.attributes = params[:entry] # # if @entry.save # flash[:notice] = "Entry was successfully updated" # redirect_to :action => "show", :id => @entry # else # render_scaffold('edit') # end # end # end # # The <tt>render_scaffold</tt> method will first check to see if you've made your own template (like "weblog/show.rhtml" for # the show action) and if not, then render the generic template for that action. This gives you the possibility of using the # scaffold while you're building your specific application. Start out with a totally generic setup, then replace one template # and one action at a time while relying on the rest of the scaffolded templates and actions. module ClassMethods # Adds a swath of generic CRUD actions to the controller. The +model_id+ is automatically converted into a class name unless # one is specifically provide through <tt>options[:class_name]</tt>. So <tt>scaffold :post</tt> would use Post as the class # and @post/@posts for the instance variables. # # It's possible to use more than one scaffold in a single controller by specifying <tt>options[:suffix] = true</tt>. This will # make <tt>scaffold :post, :suffix => true</tt> use method names like list_post, show_post, and create_post # instead of just list, show, and post. If suffix is used, then no index method is added. def scaffold(model_id, options = {}) options.assert_valid_keys(:class_name, :suffix) singular_name = model_id.to_s class_name = options[:class_name] || singular_name.camelize plural_name = singular_name.pluralize suffix = options[:suffix] ? "_#{singular_name}" : "" unless options[:suffix] module_eval <<-"end_eval", __FILE__, __LINE__ def index list end end_eval end module_eval <<-"end_eval", __FILE__, __LINE__ verify :method => :post, \:only => [ :destroy#{suffix}, :create#{suffix}, :update#{suffix} ], :redirect_to => { :action => :list#{suffix} } def list#{suffix} @#{singular_name}_pages, @#{plural_name} = paginate :#{plural_name}, :per_page => 10 render#{suffix}_scaffold "list#{suffix}" end def show#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) render#{suffix}_scaffold end def destroy#{suffix} #{class_name}.find(params[:id]).destroy redirect_to :action => "list#{suffix}" end def new#{suffix} @#{singular_name} = #{class_name}.new render#{suffix}_scaffold end def create#{suffix} @#{singular_name} = #{class_name}.new(params[:#{singular_name}]) if @#{singular_name}.save flash[:notice] = "#{class_name} was successfully created" redirect_to :action => "list#{suffix}" else render#{suffix}_scaffold('new') end end def edit#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) render#{suffix}_scaffold end def update#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) @#{singular_name}.attributes = params[:#{singular_name}] if @#{singular_name}.save flash[:notice] = "#{class_name} was successfully updated" redirect_to :action => "show#{suffix}", :id => @#{singular_name} else render#{suffix}_scaffold('edit') end end private def render#{suffix}_scaffold(action=nil) action ||= caller_method_name(caller) # logger.info ("testing template:" + "\#{self.class.controller_path}/\#{action}") if logger if template_exists?("\#{self.class.controller_path}/\#{action}") render :action => action else @scaffold_class = #{class_name} @scaffold_singular_name, @scaffold_plural_name = "#{singular_name}", "#{plural_name}" @scaffold_suffix = "#{suffix}" add_instance_variables_to_assigns @template.instance_variable_set("@content_for_layout", @template.render_file(scaffold_path(action.sub(/#{suffix}$/, "")), false)) if !active_layout.nil? render :file => active_layout, :use_full_path => true else render :file => scaffold_path('layout') end end end def scaffold_path(template_name) File.dirname(__FILE__) + "/templates/scaffolds/" + template_name + ".rhtml" end def caller_method_name(caller) caller.first.scan(/`(.*)'/).first.first # ' ruby-mode end end_eval end end end end
其中module_eval的方式很不错
发表评论
-
用了TextMate才知道什么叫神级Editor
2011-03-09 04:51 57961一直用Eclipse作为开发Ruby和Java项目的IDE,但 ... -
Ruby使用OAuth登录新浪微博和豆瓣
2011-01-09 12:49 4434首先需要安装oauth这个gem包 gem install ... -
使用Passenger+nginx部署Rails
2010-12-28 15:12 50111. Install Passender gem instal ... -
markItUp+rdiscount搭建Rails下可视化Markdown编辑器
2010-12-21 17:48 5447markItUp是基于jQuery的可视化编辑器,支持Html ... -
Rails3 and MongoDB Quick Guide
2010-12-10 14:13 2753Install MongoDB Download: http: ... -
基于ruby-protobuf的rpc示例
2009-08-11 11:51 41481, 安装ruby-protobuf gem instal ... -
Ruby导出xls和csv的utf-8问题的解决
2009-02-04 15:05 6839数据库数据为utf-8格式,包括中文和拉丁文等等 导出文件xl ... -
URL/HTML/JavaScript的encode/escape
2009-01-04 13:03 9324最近经常被URL、HTML、JavaScript的encode ... -
各种排序的Ruby实现
2008-11-27 14:51 3994Θ(n^2) 1, Bubble sort def bu ... -
12月5日北京RoR活动!
2008-11-26 18:38 3017又是一年过去了,Rails在国内的发展势态良好,很多使用RoR ... -
Rails程序开发的最大问题是代码规范
2008-08-28 11:56 5518使用Rails开发大型复杂B2B应用一年了,这个项目目前开发人 ... -
Web开发大全:ROR版——推荐序
2008-07-09 00:39 2416来自http://www.beyondrails.com/bl ... -
深入ActionMailer,使用Sendmail发邮件
2008-07-03 11:41 3396来自: http://www.beyondrails.com/ ... -
Rails里如何结合ExceptionNotification配置gmail账户发邮件
2008-06-19 19:56 30821,安装ExceptionNotification rub ... -
使用coderay和railscasts样式进行代码高亮
2008-06-17 00:16 2395CodeRay是一个语法高亮的Ruby库,效率很不错。 Cod ... -
Capistrano试用
2008-06-16 19:05 19581,客户端机器安装Capistrano gem insta ... -
lighttpd真垃圾啊
2008-06-04 18:38 2533使用lighttpd+fcgi跑Rails程序,文件上传会si ... -
将gem变成plugin
2008-06-04 11:27 1801有什么样的需求就有什么样的对策 当vhost上的帐号没有ge ... -
在Rails里使用ReCaptcha添加验证码
2008-06-03 15:51 42661,去http://recaptcha.net/sign up ... -
Rails里给文件上传添加progress_bar
2008-05-27 17:00 2087文件上传很慢时,UI没有什么用户提示,这样让人很费解,所以我们 ...
相关推荐
Rails 作为 Ruby 的主要应用框架之一,两者密切相关。 在压缩包的文件名称列表中,只有一个条目 "rails",这可能意味着压缩包内包含了 Rails 框架的核心文件,如 gemspec 文件、库文件、初始化脚本等。开发者可以...
《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别是那些想要从零开始掌握这项技术的新手。 #### 二、...
Rails指南中文版是针对Ruby on Rails框架的一份详尽教程,旨在帮助开发者深入理解并熟练掌握这个强大的Web应用开发工具。Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-...
Rails的scaffolding功能能快速生成一个基本的CRUD(创建、读取、更新、删除)应用界面,是快速原型开发的好帮手。 九、Rails的安全性 Rails提供了许多安全特性,如CSRF防护、XSS防护和参数过滤。了解这些安全措施并...
《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...
你将学习一切Rails scaffolding的基本原理,以创建自定义的交互式网络应用程序,全部使用Rails的一套丰富的工具和MVC框架。 你将掌握数据库交互、Ajax和XML的集成、丰富的内容,甚至数据的动态图形——曾经要使用...
Rails是Ruby语言的一个著名Web开发框架,全称为Ruby on Rails,它遵循MVC(Model-View-Controller)架构模式,旨在提高开发效率和代码可读性。本示例"rails项目起步示例"是一个购物系统,非常适合初学者入门学习。 ...
《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入的...
Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...
首先,Rails 3.1是Ruby on Rails框架的一个版本,它在2011年发布。这个版本引入了一些显著的改进,如Asset Pipeline(资产管道)和CoffeeScript支持。Asset Pipeline允许开发者更有效地管理和优化应用程序的前端资源...
MVC是Rails的核心架构之一,这一章节将详细介绍这三个组件的作用和相互关系。模型负责与数据库交互,管理数据;视图用于展示数据给用户;控制器则处理用户请求,协调模型和视图之间的操作。了解并正确运用MVC模式是...
1. **ActiveRecord**:这是Rails的核心组件之一,负责数据库交互。它实现了对象关系映射(ORM),将数据库表映射为Ruby类,使得开发者可以像操作普通对象一样操作数据库记录。 2. **ActionController**:处理HTTP...
10. **Scaffolding**:在`sample_app`中,你可能会看到一些由`rails generate scaffold`命令自动生成的代码,这是一种快速构建基本CRUD(创建、读取、更新、删除)功能的方法。 11. **安全**:`sample_app`可能包含...
Rails之道.pdf 高清 带书签
在本项目"Ruby-Rails实战之B2C商城开发"中,我们将深入探索使用Ruby on Rails这一强大的Web开发框架来构建一个完整的B2C(Business-to-Consumer)在线商城。Rails是Ruby语言的一个核心框架,以其MVC(Model-View-...
标题 "Rails" 指的是 Ruby on Rails,一个开源的Web应用程序框架,它基于Ruby编程语言,遵循MVC(模型-视图-控制器)架构模式。Rails由David Heinemeier Hansson在2004年创建,其设计理念是强调代码的简洁性、DRY...
Ruby on Rails,简称Rails,是一种基于Ruby语言的开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,极大地简化了Web应用的开发过程。Rails的哲学是“约定优于配置”,鼓励开发者遵循一套标准的...
标题中的“Web开发敏捷之道--应用Rails进行敏捷Web开发 之 Depot代码”表明这是一个关于使用Ruby on Rails框架进行敏捷Web开发的示例项目,名为Depot。Ruby on Rails(简称Rails)是一个开源的Web应用程序框架,它...