Rails Generate 列示

本文介绍了使用Rails进行快速应用开发的方法,包括如何生成控制器、模型、迁移文件等,并提供了多个示例来展示不同类型的生成任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应该是出自Netbean的generate介绍,例子还不错,有些时候能够节省时间

[size=large]controller[/size]

[quote]Stubs out a new controller and its views. Pass the controller name, either CamelCased or under_scored, and a list of views as arguments.

To create a controller within a module, specify the controller name as a path like parent_module/controller_name.

This generates a controller class in app/controllers, view templates in app/views/controller_name, a helper class in app/helpers, a functional test suite in test/functional and a helper test suite in test/unit/helpers.
[/quote]


./script/generate controller CreditCard open debit credit 
#Credit card controller with URLs like /credit_card/debit.
close



[quote] Controller: app/controllers/credit_card_controller.rb
Functional Test: test/functional/credit_card_controller_test.rb
Views: app/views/credit_card/debit.html.erb [...]
Helper: app/helpers/credit_card_helper.rb
Helper Test: test/unit/helpers/credit_card_helper_test.rb[/quote]

带Modules 的例子

  ./script/generate controller 'admin/credit_card' suspend late_fee
#Credit card admin controller with URLs /admin/credit_card/suspend.


[quote] Controller: app/controllers/admin/credit_card_controller.rb
Functional Test: test/functional/admin/credit_card_controller_test.rb
Views: app/views/admin/credit_card/debit.html.erb [...]
Helper: app/helpers/admin/credit_card_helper.rb
Helper Test: test/unit/helpers/admin/credit_card_helper_test.rb[/quote]

[size=large]integration_test[/size]
介绍:
[quote]
Stubs out a new integration test. Pass the name of the test, either CamelCased or under_scored, as an argument. The new test class is generated in test/integration/testname_test.rb
[/quote]

示例

./script/generate integration_test GeneralStories
#creates a GeneralStories integration test in test/integration/general_stories_test.rb


[size=large]model[/size]


[quote]Stubs out a new model. Pass the model name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the model immediately.

This generates a model class in app/models, a unit test in test/unit, a test fixture in test/fixtures/singular_name.yml, and a migration in db/migrate.[/quote]


 ./script/generate model account
#creates an Account model, test, fixture, and migration:

[quote] Model: app/models/account.rb
Test: test/unit/account_test.rb
Fixtures: test/fixtures/accounts.yml
Migration: db/migrate/XXX_add_accounts.rb[/quote]
  ./script/generate model post title:string body:text published:boolean
#creates a Post model with a string title, text body, and published flag.



[size=large]migration[/size]


[quote]Stubs out a new database migration. Pass the migration name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

A migration class is generated in db/migrate prefixed by a timestamp of the current date and time.

You can name your migration in either of these formats to generate add/remove column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable[/quote]


  ./script/generate migration AddSslFlag
#If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration db/migrate/20080514090912_add_ssl_flag.rb

./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`


This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with this in the Up migration:

[quote]add_column :posts, :title, :string
add_column :posts, :body, :text
add_column :posts, :published, :boolean
And this in the Down migration:

remove_column :posts, :published
remove_column :posts, :body
remove_column :posts, :title
[/quote]
[size=large]mailer[/size]


[quote]Stubs out a new mailer and its views. Pass the mailer name, either CamelCased or under_scored, and an optional list of emails as arguments.

This generates a mailer class in app/models, view templates in app/views/mailer_name, a unit test in test/unit, and fixtures in test/fixtures.[/quote]


  ./script/generate mailer Notifications signup forgot_password invoice
#creates a Notifications mailer class, views, test, and fixtures:


[quote] Mailer: app/models/notifications.rb
Views: app/views/notifications/signup.erb [...]
Test: test/unit/test/unit/notifications_test.rb
Fixtures: test/fixtures/notifications/signup [...][/quote]

[size=large]plugin[/size]


[quote]Stubs out a new plugin. Pass the plugin name, either CamelCased or under_scored, as an argument. Pass –with-generator to add an example generator also.

This creates a plugin in vendor/plugins including an init.rb and README as well as standard lib, task, and test directories.[/quote]


   ./script/generate plugin BrowserFilters
#creates a standard browser_filters plugin:


vendor/plugins/browser_filters/README
vendor/plugins/browser_filters/init.rb
vendor/plugins/browser_filters/install.rb
vendor/plugins/browser_filters/lib/browser_filters.rb
vendor/plugins/browser_filters/test/browser_filters_test.rb
vendor/plugins/browser_filters/tasks/browser_filters_tasks.rake
./script/generate plugin BrowserFilters --with-generator
creates a browser_filters generator also:

vendor/plugins/browser_filters/generators/browser_filters/browser_filters_generator.rb
vendor/plugins/browser_filters/generators/browser_filters/USAGE
vendor/plugins/browser_filters/generators/browser_filters/templates/

[size=large]scaffold[/size]


[quote]Scaffolds an entire resource, from model and migration to controller and views, along with a full test suite. The resource is ready to use as a starting point for your RESTful, resource-oriented application.

Pass the name of the model (in singular form), either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the resource immediately.

For example, scaffold post title:string body:text published:boolean gives you a model with those three attributes, a controller that handles the create/show/update/destroy, forms to create and edit your posts, and an index that lists them all, as well as a map.resources :posts declaration in config/routes.rb.

If you want to remove all the generated files, run script/destroy scaffold ModelName.
[/quote]


./script/generate scaffold post
./script/generate scaffold post title:string body:text published:boolean
./script/generate scaffold purchase order_id:integer amount:decimal


[size=large]session_migration[/size]


[quote]Creates a migration to add the sessions table used by the Active Record session store. Pass the migration name, either CamelCased or under_scored, as an argument.[/quote]


 ./script/generate session_migration CreateSessionTable
#With 4 existing migrations, this creates the AddSessionTable migration in db/migrate/005_add_session_table.rb



[size=large]metal[/size]


Cast some metal!


  ./script/generate metal poller
#This will create: Metal: app/metal/poller.rb



[size=large]observer[/size]


Stubs out a new observer. Pass the observer name, either CamelCased or under_scored, as an argument.

The generator creates an observer class in app/models and a unit test in test/unit.



  ./script/generate observer Account
#creates an Account observer and unit test:


Observer: app/models/account_observer.rb
Test: test/unit/account_observer_test.rb

[size=large]resource[/size]


[quote]Stubs out a new resource including an empty model and controller suitable for a restful, resource-oriented application. Pass the singular model name, either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the resource immediately.

This creates a model, controller, helper, tests and fixtures for all of them, and the corresponding map.resources declaration in config/routes.rb

Unlike the scaffold generator, the resource generator does not create views or add any methods to the generated controller.
[/quote]


  ./script/generate resource post # no attributes
./script/generate resource post title:string body:text published:boolean
./script/generate resource purchase order_id:integer amount:decimal


[size=large]helper[/size]


[quote]Stubs out a new helper. Pass the helper name, either CamelCased or under_scored.

To create a helper within a module, specify the helper name as a path like parent_module/helper_name.

This generates a helper class in app/helpers and a helper test suite in test/unit/helpers.[/quote]


  ./script/generate helper CreditCard
#Credit card helper.


Helper: app/helpers/credit_card_helper.rb
Test: test/unit/helpers/credit_card_helper_test.rb
Modules


  ./script/generate helper 'admin/credit_card'
#Credit card admin helper.


Helper: app/helpers/admin/credit_card_helper.rb
Test: test/unit/helpers/admin/credit_card_helper_test.rb

[size=large]performance_test[/size]


[quote]Stubs out a new performance test. Pass the name of the test, either CamelCased or under_scored, as an argument. The new test class is generated in test/performance/testname_test.rb[/quote]


 ./script/generate performance_test GeneralStories
#creates a GeneralStories performance test in
test/performance/general_stories_test.rb
基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值