- 浏览: 58437 次
- 性别:
- 来自: 深圳
最新评论
之前一直找这个问题的解决办法,但一直没找到,在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 980Diaspora Function Features List ... -
ruby语法-传值
2011-08-23 16:49 1021=begin def test1(*a) p a.firs ... -
Rails 3 用 JQuery 替代 Prototype
2011-07-28 22:48 10991.进入到工程目录中修改Gemfile文件,加入如下行: g ... -
Generate a list of Rails controllers and methods
2011-07-01 17:40 824列出项目中的controler和他对应的actions: @c ... -
Ubuntu手动安装JDK
2011-05-20 16:03 540Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
bundler
2011-04-25 12:12 803Rails3里多了个Bundler工具。 它是一个Librar ... -
研究小计1
2011-04-20 00:55 631有时候对一些基础的东西,不知道它的边界在哪里,其实做些小实验就 ... -
RubyGem version error: rack(1.0.0 not ~> 1.0.1)
2011-04-17 00:09 1328I have this error when I try to ... -
rubyonrails Restful Authentication 插件
2011-04-17 00:01 781restful_authentication是Rails的一个 ... -
XP.CMD命令大全
2011-04-17 00:00 610有关某个命令的详细信 ... -
几个有用的方法
2011-04-16 23:58 558hash.invert Returns a new hash ... -
Firefox键盘快捷方式
2011-04-16 23:55 1186Firefox键盘快捷方式 键盘快捷健 下面是 Mozil ... -
Ubuntu手动安装JDK
2011-04-16 23:53 708Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
字符转时间格式转换成时间对象
2011-04-16 23:52 718Time.parse("Wed, 23 Jan 20 ... -
使用Rails时遇到了服务器启动问题 script/server:3
2011-04-16 23:47 1261境 Windows Vista Home Premium c ... -
DoDirectPayment
2011-04-16 23:44 972DoDirectPayment 1. DoDirectPay ... -
Rails异常处理
2011-04-16 23:43 944异常处理是开发过程中 ... -
ruby和rails中的回调函数
2011-04-16 23:41 1853Ruby中Class,Object,Module这三个类定义了 ... -
深入理解alias, alias_method和alias_method_chain
2011-04-16 23:41 853对 于alias, alias_method, alias_m ... -
Rails3:使用bundler管理gems
2011-04-16 23:39 1369bundler是一套为了 Rails3 所打造的全新 Gem ...
相关推荐
This short early adopter book details both how you'll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
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 ...
Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7:...
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 ...
This short early adopter book details both how you’ll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
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 ...
this unique book approaches Rails development from the outer layer: the application interface. You'll learn how to create something visible with Rails before reaching the more difficult database ...
graphql-rails-generators 一些生成器可以轻松将Rails模型与集成。 我之所以创建它,是因为我浪费了太多的按键来手动复制模型架构以创建graphql类型。 该项目包含用于查看ActiveRecord模型架构的生成器,并为您...
not just Rails but all the open source technologies that go along with it. This book will be your guide as you navigate this new terrain. For a .NET developer, learning Rails is as much about the ...
Rails Plugins:Extending Rails Beyond the Core
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 2.0 english version
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 ...
学习ruby!
Rather than toss you into the middle of the framework’s Model-View-Controller architecture, as many books do, Learning Rails 5 begins with the foundations of the Web you already know. You’ll learn ...
The Rails way 最新版本 The Rails way 最新版本 The Rails way 最新版本
You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...
这将为您的项目创建一个新文件夹,请确保在所有文件中搜索Rails6Scaffold和rails6_scaffold ,因为在某些地方您需要手动更改名称。 这是找到的文件列表,在运行“重命名” gem后需要手动更新: app/views/layouts...
This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...