【轉】Ruby on Rails,建立最簡單的視圖/控制器單元Hello World

http://blog.csdn.net/abbuggy/article/details/7409445javascript

進入應用目錄simple_site,使用生成命令rails generate。這個命令負責在當前目錄按照參數要求生成目錄結構及代碼。不帶任何參數的命令會顯示提示信息。css

 

E:\greensoft\RailsInstaller\Sites\simple_site>rails generate 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名稱列表以參數的方式給出。後面還舉了一個很典型的例子。html

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。java

 

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。jquery

 

class DemoController < ApplicationController def index end end

 

代碼編寫完畢,啓動web服務器。在瀏覽器地址欄中輸入:localhost:3000/demo/indexweb

相關文章
相關標籤/搜索