- 浏览: 59926 次
- 性别:
- 来自: 深圳
最新评论
之前一直找这个问题的解决办法,但一直没找到,在g上看到了一个人发的博客,虽然不是理想的实现,但至少有人和我有一样的需求,欣慰!
博客内容:
rails scaffold getting the column list from existing table
Starting with rails 2, for scaffold generation you are forced to pass the column name and data type of each field you want generated. This is fine for new tables, but if you have a table or have added dozens of columns to a table and want to regenerate, and you are lazy like me, typing this long list of columns is a pain. So this script will generate the input you need to pass the generator.
For example, you have a table called "resellers" and it has a bunch of columns, you could do:
ruby script/generate scaffold reseller first_name:string last_name:string address:string city:string ........ and keep on typing
or you can do:
ruby script/console
name = 'Resellers'
si_table_name = 'Resellers'
si_field_names = Array.new
si_cols = ActiveRecord::Base.connection.columns(si_table_name,
"#{name} Columns")
si_cols.each do |c|
si_field_names << "#{c.name}:#{c.type}"
end
puts si_field_names.join(' ')
It prints:
id:integer first_name:string last_name:string address:string city:string state:string postal_code:string country:string ....
now with the magic of cut and paste you can generate the scaffolding how you want without typing all this in.
来源:http://jnylund.typepad.com/joels_blog/2010/03/rails-scaffold-getting-the-column-list-from-existing-table.html
博客内容:
rails scaffold getting the column list from existing table
Starting with rails 2, for scaffold generation you are forced to pass the column name and data type of each field you want generated. This is fine for new tables, but if you have a table or have added dozens of columns to a table and want to regenerate, and you are lazy like me, typing this long list of columns is a pain. So this script will generate the input you need to pass the generator.
For example, you have a table called "resellers" and it has a bunch of columns, you could do:
ruby script/generate scaffold reseller first_name:string last_name:string address:string city:string ........ and keep on typing
or you can do:
ruby script/console
name = 'Resellers'
si_table_name = 'Resellers'
si_field_names = Array.new
si_cols = ActiveRecord::Base.connection.columns(si_table_name,
"#{name} Columns")
si_cols.each do |c|
si_field_names << "#{c.name}:#{c.type}"
end
puts si_field_names.join(' ')
It prints:
id:integer first_name:string last_name:string address:string city:string state:string postal_code:string country:string ....
now with the magic of cut and paste you can generate the scaffolding how you want without typing all this in.
来源:http://jnylund.typepad.com/joels_blog/2010/03/rails-scaffold-getting-the-column-list-from-existing-table.html
发表评论
-
Diaspora 功能特性列表
2013-05-10 14:25 1018Diaspora Function Features List ... -
ruby语法-传值
2011-08-23 16:49 1051=begin def test1(*a) p a.firs ... -
Rails 3 用 JQuery 替代 Prototype
2011-07-28 22:48 11301.进入到工程目录中修改Gemfile文件,加入如下行: g ... -
Generate a list of Rails controllers and methods
2011-07-01 17:40 851列出项目中的controler和他对应的actions: @c ... -
Ubuntu手动安装JDK
2011-05-20 16:03 568Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
bundler
2011-04-25 12:12 829Rails3里多了个Bundler工具。 它是一个Librar ... -
研究小计1
2011-04-20 00:55 660有时候对一些基础的东西,不知道它的边界在哪里,其实做些小实验就 ... -
RubyGem version error: rack(1.0.0 not ~> 1.0.1)
2011-04-17 00:09 1357I have this error when I try to ... -
rubyonrails Restful Authentication 插件
2011-04-17 00:01 811restful_authentication是Rails的一个 ... -
XP.CMD命令大全
2011-04-17 00:00 636有关某个命令的详细信 ... -
几个有用的方法
2011-04-16 23:58 587hash.invert Returns a new hash ... -
Firefox键盘快捷方式
2011-04-16 23:55 1214Firefox键盘快捷方式 键盘快捷健 下面是 Mozil ... -
Ubuntu手动安装JDK
2011-04-16 23:53 729Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
字符转时间格式转换成时间对象
2011-04-16 23:52 760Time.parse("Wed, 23 Jan 20 ... -
使用Rails时遇到了服务器启动问题 script/server:3
2011-04-16 23:47 1287境 Windows Vista Home Premium c ... -
DoDirectPayment
2011-04-16 23:44 998DoDirectPayment 1. DoDirectPay ... -
Rails异常处理
2011-04-16 23:43 962异常处理是开发过程中 ... -
ruby和rails中的回调函数
2011-04-16 23:41 1881Ruby中Class,Object,Module这三个类定义了 ... -
深入理解alias, alias_method和alias_method_chain
2011-04-16 23:41 884对 于alias, alias_method, alias_m ... -
Rails3:使用bundler管理gems
2011-04-16 23:39 1393bundler是一套为了 Rails3 所打造的全新 Gem ...
相关推荐
Since the API documentation is liberally licensed (just like the rest of Rails), there are some sections of the book that draw from the API documentation. But in practically all of those cases, the ...
### 关于《The Rails 4 Way》的知识点总结 #### 标题:The Rails 4 Way 这本书主要讲述了Ruby on Rails 4版本的核心特性和最佳实践。Ruby on Rails(简称Rails)是一个用Ruby语言编写的开源全栈Web应用框架。本书...
标题中的“用Ext Scaffold插件打造Rails的Ext风格”指的是在Ruby on Rails(Rails)框架中,使用Ext JS库和一个名为“Ext Scaffold”的插件来创建图形用户界面(GUI)。Ext JS是一个流行的JavaScript库,提供了丰富...
《The Rails Way》一书由Obie Fernandez撰写,属于Addison-Wesley Professional Ruby系列,该系列致力于为读者提供实用、面向人且深入的信息,帮助他们利用Ruby平台创建动态技术解决方案。这一系列书籍的创立基于一...
Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...
graphql-rails-generators 一些生成器可以轻松将Rails模型与集成。 我之所以创建它,是因为我浪费了太多的按键来手动复制模型架构以创建graphql类型。 该项目包含用于查看ActiveRecord模型架构的生成器,并为您...
Embrace the full stack of web development, from styling with Bootstrap, building an interactive user interface with Angular 2, to storing data quickly and reliably in PostgreSQL. With this fully ...
Rails Plugins:Extending Rails Beyond the Core
作者特别提到了“CRUD懶人大法Scaffold”,它是一种通过Rails自动生成代码的方式来快速搭建基本的CRUD操作,这大大简化了开发流程,使得开发者可以将更多的精力放在业务逻辑的实现上。 此外,书中还介绍了一些Rails...
Rails3 是 Ruby on Rails 框架的一个版本,它提供了一系列强大的命令行工具,使得开发者可以快速地构建和管理Web应用。在本文中,我们将深入探讨Rails3中的常用命令,帮助你更高效地进行开发工作。 首先,新建一个...
ruby rails demo, rails 简单demo。 (1)到ruby官网:http://www.ruby-lang.org/en/下载window安装包,并安装, ruby版本: ruby -v (2)安装Rails3 gem install rails (3)安装sqlite3 gem install sqlite3-ruby 安装...
For those new to Rails, this book provides a quick introduction, the big picture, a walk through the installation process, and some tips on getting started. If you've already started working with ...
"rails-scaffold-react" 是一个基于 Ruby on Rails 的应用程序模板,专为集成 React.js 前端库而设计。这个脚手架可以帮助开发者快速搭建一个包含 Rails 后端和 React 前端的完整Web应用。它利用了 Rails 的强大功能...