`

Rails Generate 列示

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

controller

引用
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.



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



引用
  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


带Modules 的例子

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


引用
  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


integration_test
介绍:
引用

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


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


model


引用
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.




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

引用
  Model:      app/models/account.rb
  Test:       test/unit/account_test.rb
  Fixtures:   test/fixtures/accounts.yml
  Migration:  db/migrate/XXX_add_accounts.rb

  ./script/generate model post title:string body:text published:boolean
#creates a Post model with a string title, text body, and published flag.



migration


引用
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



  ./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:

引用
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

mailer


引用
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.




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


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


plugin


引用
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.



   ./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/

scaffold


引用
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.



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


session_migration


引用
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.




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



metal


Cast some metal!



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



observer


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

resource


引用
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.



  ./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


helper


引用
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.




  ./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

performance_test


引用
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




 ./script/generate performance_test GeneralStories
#creates a GeneralStories performance test in 
test/performance/general_stories_test.rb
分享到:
评论

相关推荐

    Ruby on Rails入门例子

    2. 创建资源:使用`rails generate controller Posts`生成控制器,再用`rails generate scaffold Post title:string content:text`生成模型和相关的控制器、视图及路由。 3. 迁移数据库:编写迁移文件,更新数据库表...

    ruby rails demo

    ruby rails demo, rails 简单demo。 ...(2)安装Rails3 gem install rails (3)安装sqlite3 gem install ...rails generate scaffold Post name:string title:string content:text rake routes 显示路由config/routes.rb

    中文版rails教程

    1. **生成资源(Generating Resources)**:使用`rails generate`命令可以快速创建模型、控制器、视图等文件,如`rails generate scaffold Post title:string content:text`会生成一个名为Post的资源,包含相关的...

    Ruby on Rails实例开发

    接着,使用Rails的生成器创建控制器、模型和视图,如`rails generate controller`或`rails generate model`。然后,配置数据库连接,编写数据库迁移(migrations)以定义数据表结构,执行`rake db:migrate`来应用...

    Ruby+for+Rails

    `rails generate migration AddColumnToBooks column:type`会生成一个新的迁移文件,然后通过`rails db:migrate`执行迁移,更新数据库。 7. **路由(Routing)** Rails的路由系统将URL映射到控制器的动作上,定义...

    Ruby on Rails Tutorial Learn Rails by Example 的源代码

    8. **生成器和迁移**:Rails提供了一些命令行工具,如`rails generate`和`rails db:migrate`,用于快速生成模型、控制器、迁移等,并执行数据库迁移。 9. **测试**:Rails鼓励TDD(测试驱动开发),`spec`目录包含...

    CanCan1Ruby2Devise3Rails4:在 Rails 4 上使用 CanCan 1 和 Devise 3 的应用程序

    installrails generate devise user ######安装CanCan: gem 'cancan'bundle install rails generate cancan : ability rails generate model role name : string rails generate migration UsersHaveAndBelongToMa

    Rails进行敏捷Web开发(所有版本的源码rails3.0-4.0)

    此外,`rails generate scaffold`命令也变得更加灵活,可以自定义模板。 4. Rails 4.0: Rails 4带来了大量的改变和优化。最显著的是加入了Strong Parameters,提高了安全性,避免了Mass Assignment漏洞。另外,它...

    centOS Rails3环境搭建

    rails generate scaffold person name:string bio:text birthday:date ``` - **模型创建与删除**: ```bash rails generate model article rails destroy model article ``` - **控制器创建**: ```bash ...

    Ruby-GoOnRails使用Rails生成器来生成一个Golang应用

    rails generate go_on_rails:app my_go_app ``` 这将在Rails项目的`lib/my_go_app`目录下创建一个新的Go应用结构。 ### 4. 集成Go API `GoOnRails`会生成一个基础的Go API服务器,你可以在此基础上编写业务逻辑。...

    rails向导打包

    7. **Rails 命令行工具和 Rake 任务**: Rails 提供了一系列命令行工具,如 `rails server`、`rails generate` 和 `rails dbconsole`,帮助开发者快速启动、生成代码和操作数据库。Rake 是一个构建工具,用于执行任务...

    Rails中应用Ext.tree:以中国的省市地区三级联动选择为例

    rails generate model City province_id:integer name:string rails generate model District city_id:integer name:string ``` 然后,通过运行`rails db:migrate`来执行数据库迁移。 在模型定义完成后,作者会...

    power-types:Platanus 团队使用的 Rails 模式强制类型

    电源类型 Platanus 团队...安装添加到您的 Gemfile: gem "power-types" bundle install电源类型服务为了生成服务,我们使用: $ rails generate service MyService foo bar这将创建 MyService 类,继承自基础服务类:

    [Rails 常用插件简介]CRUD Generator 2

    接着,你可以通过Rails的generate命令来触发CRUD Generator 2,例如: ```bash rails generate crud_generator2 ModelName ``` 这里的`ModelName`是你需要创建的模型名称。这个命令将会生成相应的模型文件、控制器...

    ruby_on_rails 源代码上

    Rails提供了一系列内置的命令行工具,如`rails new`创建新应用,`rails generate`生成模型、控制器、迁移等。这些发电机能快速生成基本结构,加速开发过程。 11. **Scaffolding** Scaffolding是Rails提供的一种...

    Rails 101S

    - 示例:通过`rails generate scaffold Topic title:string description:text`创建一个Topic模型及其关联的CRUD操作。 - **整合Bootstrap与版型设置**:介绍如何将Bootstrap框架集成到Rails应用中,并设置基础版型...

    rails学习教程

    Rails提供了许多命令行工具,如`rails generate`,可以自动生成模型、控制器、迁移等,大大减少了手动编码的工作量。 六、ActiveRecord和数据库 ActiveRecord是Rails中的ORM(对象关系映射)库,它自动处理数据库...

    rails操作.docx

    通过`rails generate`命令,可以自动生成控制器、模型、视图等相关文件,例如创建一个名为`autoweb`的应用,只需执行`rails generate controller Autoweb`,Rails会自动创建相关文件,开发者只需要填充具体业务逻辑...

    Rails101_by_rails4.0

    《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

Global site tag (gtag.js) - Google Analytics