【转】Ruby on Rails,创建最简单的视图/控制器单元Hello World
http://blog.csdn.net/abbuggy/article/details/7409445
在
- Usage: rails generate GENERATOR [args] [options]
- General options:
- -h, [--help] # Print generator's options and usage
- -p, [--pretend] # Run but do not make any changes
- -f, [--force] # Overwrite files that already exist
- -s, [--skip] # Skip files that already exist
- -q, [--quiet] # Suppress status output
- Please choose a generator below.
- Rails:
- assets
- controller
- generator
- helper
- integration_test
- mailer
- migration
- model
- observer
- performance_test
- resource
- scaffold
- scaffold_controller
- session_migration
- task
- Coffee:
- coffee:assets
- Jquery:
- jquery:install
- Js:
- js:assets
- 我们需要创建Controller和View,使用rails generate controller命令。不带任何参数的命令会对创建controller的使用方式进行提示,应该静下心来读一读。其中最有用的部分是说,以 CamelCase(大写字母开头分割单词)或者under_scored(下划线分割单词)的方式命名Controller,将View名称列表以参数 的方式给出。后面还举了一个很典型的例子。
- E:\greensoft\RailsInstaller\Sites\simple_site>rails generate controller
- Usage:
- rails generate controller NAME [action action] [options]
- Options:
- [--skip-namespace] # Skip namespace (affects only isolated applications)
- [--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
- -e, [--template-engine=NAME] # Template engine to be invoked
- # Default: erb
- -t, [--test-framework=NAME] # Test framework to be invoked
- # Default: test_unit
- [--helper] # Indicates when to generate helper
- # Default: true
- [--assets] # Indicates when to generate assets
- # Default: true
- Runtime options:
- -f, [--force] # Overwrite files that already exist
- -p, [--pretend] # Run but do not make any changes
- -q, [--quiet] # Supress status output
- -s, [--skip] # Skip files that already exist
- Description:
- 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 and invokes helper,
- template engine and test framework generators.
- Example:
- `rails generate controller CreditCard open debit credit close`
- Credit card controller with URLs like /credit_card/debit.
- 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
我
们要创建一个名为demo的Controller,指向名为index的View。在这里可以使用g来代替generate,就像s代替service一
样。回显信息告诉我们,rails为我们在app/controller目录中创建了一个名为demo_controller的Controller。
app/views/目录中创建了一个名为demo的目录,目录中的index.html.erb对应View。
- E:\greensoft\RailsInstaller\Sites\simple_site>rails g controller demo index
- create app/controllers/demo_controller.rb
- route get "demo/index"
- invoke erb
- create app/views/demo
- create app/views/demo/index.html.erb
- invoke test_unit
- create test/functional/demo_controller_test.rb
- invoke helper
- create app/helpers/demo_helper.rb
- invoke test_unit
- create test/unit/helpers/demo_helper_test.rb
- invoke assets
- invoke coffee
- create app/assets/javascripts/demo.js.coffee
- invoke scss
- create app/assets/stylesheets/demo.css.scss
controller目录中名为demo的Controller里有一个名为index的方法,在在这里应该叫做action。这个叫index的action,对应views目录中名为index.html.erb的View。
- class DemoController < ApplicationController
- def index
- end
- end
修改一下页面内容
- <h1>Demo#index</h1>
- <p>Hello World!</p>
- <p>Find me in app/views/demo/index.html.erb</p>
代码编写完毕,启动web服务器。在浏览器地址栏中输入:localhost:3000/demo/index。里程碑!