之前一直找这个问题的解决办法,但一直没找到,在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