Ruby on Rails,创建最简单的视图/控制器单元Hello World

作者: 边晓宇
发布时间:2015-07-13 11:26:53

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

Ruby on Rails,创建一个最简单的站点中我们通过rails new simple_site命令创建了一个web应用骨架。笔墨纸砚都准备好了,该是时候写出第一个Hello World啦~!

Rails明确的按照MVC结构组织内容,回想一下MVC结构的简单知识。Browser(浏览器)向Controller(控制器)发出交互请求;Controller对请求做出分发处理调用Model(模型)进行业务或者数据运算;Controller将Model运算的结果推送给View(视图)使其在Browser中绘制显示。作为Hello World来说,建立一个从Browser到Controller再到View的例子既简单有很能说明问题。也就是说这个例子中不涉及与模型及数据库的任何操作,只是由Controller根据Browser发来的请求进行相应,并将相应交给View显示在Browser上。


进入应用目录simple_site,使用生成命令rails generate。这个命令负责在当前目录按照参数要求生成目录结构及代码。不带任何参数的命令会显示提示信息。

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名称列表以参数的方式给出。后面还举了一个很典型的例子。

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。里程碑!


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

来源:http://www.cnblogs.com/abbuggy/archive/2012/03/30/2594226.ht

推荐: