Ruby for Rails 最佳实践Ⅱ

第二章 Rails 工作原理

一、Rails 的 MVC 框架设计实现概览

MVC 阶段

Rails 子库

目的

模型

ActiveRecord

在关系数据库表和操作数据库记录的Ruby程序代码之间提供接口和绑定。Ruby方法名自动从数据库表的字段名产生,等等

视图

ActionView

一个基于嵌入式Ruby(ERb)的系统,用于定义服务于数据表示的表示模板。对Rails应用的每一次Web连接都导致显示一个视图

控制器

ActionController

在ActiveRecord(数据库接口)和ActionView(表示引擎)之间的数据中介。ActionController提供功能设施,处理和组织来自数据库和Web表单输入的数据,然后它将这些数据递交给ActionView,ActionView将这些数据插入模板并显示

 

二、图解 Ruby 和 Rails 框架

 

三、全程开发 Rails 应用,音乐店应用——R4Rmusic(PS:完整版攻略)

Google Code 下载:http://kerryas.googlecode.com/files/R4Rmusic1.rar

 

1. 生成音乐店的数据库表 SQL 命令

CREATE DATABASE r4rmusic1;

USE r4rmusic1;

DROP TABLE IF EXISTS works;

DROP TABLE IF EXISTS editions;

DROP TABLE IF EXISTS composers;

CREATE TABLE works (

  id INT(11) NOT NULL AUTO_INCREMENT,

  composer_id INT(11),

  title VARCHAR(100),

  PRIMARY KEY (id)

);

CREATE TABLE editions (

  id INT(11) NOT NULL AUTO_INCREMENT,

  work_id INT(11) NOT NULL,

  description VARCHAR(30),

  publisher VARCHAR(60),

  YEAR INT(4),

  price FLOAT,

  PRIMARY KEY  (id)

);

CREATE TABLE composers (

  id INT(11) NOT NULL AUTO_INCREMENT,

  first_name VARCHAR(25),

  last_name VARCHAR(25),

  PRIMARY KEY (id)

);

 

2. 产生音乐店示例库存数据的 SQL 命令

INSERT INTO composers

  VALUES (1,"Johannes","Brahms");

INSERT INTO composers

  VALUES (2,"Claude","Debussy");

 

INSERT INTO works

  VALUES (1,1,"Sonata for Cello and Piano in F Major");

INSERT INTO works

  VALUES (2,2,"String Quartet");

 

INSERT INTO editions

  VALUES (1,1,"Facsimile","D. Black Music House", 1998, 21.95);

INSERT INTO editions

  VALUES (2,1,"Urtext","RubyTunes, Inc.", 1977, 23.50);

INSERT INTO editions

  VALUES (3,1,"ed. Y.Matsumoto","RubyTunes, Inc.", 2001, 22.95);

INSERT INTO editions

  VALUES (4,2,"","D. Black Music House", 1995, 39.95);

INSERT INTO editions

  VALUES (5,2,"Reprint of 1894 ed.", "RubyTunes, Inc.", 2003, 35.95);

 

3. 创建 R4Rmusic1 项目(数据库基于Mysql)

F:\ruby_project>rails -d mysql R4Rmusic1

F:\ruby_project>cd R4Rmusic1

 

4. 初始设置及测试

(1)配置数据库文件 config/database.yml

development:

  adapter: mysql

  encoding: utf8

  reconnect: false

  database: R4Rmusic1

  pool: 5

  username: root

  password:

  host: localhost

 

(2)启动 WEBrick 应用服务器

F:\ruby_project\R4Rmusic1>ruby script\server

=> Booting WEBrick

=> Rails 2.3.3 application starting on http://0.0.0.0:3000

=> Call with -d to detach

=> Ctrl-C to shutdown server

[2009-08-16 14:40:31] INFO  WEBrick 1.3.1

[2009-08-16 14:40:31] INFO  ruby 1.8.6 (2008-08-11) [i386-mswin32]

[2009-08-16 14:40:31] INFO  WEBrick::HTTPServer#start: pid=988 port=3000

 

(3)测试服务

访问:http://localhost:3000/

点击:About your application’s environment 测试是否列出应用的环境列表,如果是则服务正确,如果出错需要访问log/development.log 查找原因。

 

5. 创建 Rails 模型文件

F:\ruby_project\R4Rmusic1>ruby script/generate model work

F:\ruby_project\R4Rmusic1>ruby script/generate model edition

F:\ruby_project\R4Rmusic1>ruby script/generate model composer

执行后会在 app/model 目录下生成这三个模型文件:work.rb, edition.rb, composer.rb

 

将 work.rb 修改如下:

class Work < ActiveRecord::Base

         belongs_to :composer

         has_many :editions

end

 

将 edition.rb 修改如下:

class Edition < ActiveRecord::Base

         belongs_to :work

end

 

将 composer.rb 修改如下:

class Composer < ActiveRecord::Base

         has_many :works

end

 

6. 创建访问主界面

(1)创建主界面视图及控制器

F:\ruby_project\R4Rmusic1>ruby script/generate controller main welcome

 

(2)将 app\controllers\main_controller.rb 修改如下(编写控制器):

class MainController < ApplicationController

  def welcome

       #@composers = Composer.find(:all)

       @composers = Composer.find(:all).sort_by {|c| [c.last_name, c.first_name]}

  end

end

 

(3)将 app\views\main\welcome.html.erb 修改如下(编写视图):

<p>Click on a composer's name

to see all of that composer's works.</p>

<ul>

  <% @composers.each do |composer| %>

  <li><%= link_to "#{composer.first_name} #{composer.last_name}",

                  :controller => "composer",

                  :action     => "show",

                  :id         => composer.id %>

  </li>

  <% end %>

</ul>

 

(4)R4RMusic基本布局(在app\views\layouts 中创建base.html.erb)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

  <title><%= @page_title %></title>

</head>

<body>

<h1 class="banner">The R4R Music Store</h1>

   <%= @content_for_layout %>

<hr/>

<p>Copyright &copy; 2006, R4RMusic</p>

</body>

</html>

 

(5)将默认布局加入到 app\controllers\ application_controller.rb 中

class ApplicationController < ActionController::Base

  layout "base"

end

 

(6)设置应用程序入口文件,在 config/routes.rb 中加入配置:

map.connect '', :controller => "main", :action => "welcome"

 

(7)删除public/index.html(Rails 应用默认入口),重启 WEBrick 测试。

 

7. 创建作品、版本及作曲者的控制器

F:\ruby_project\R4Rmusic1>ruby script/generate controller work show

F:\ruby_project\R4Rmusic1>ruby script/generate controller edition show

F:\ruby_project\R4Rmusic1>ruby script/generate controller composer show

 

修改 app\controllers\work_controller.rb

def show

    @work = Work.find(params[:id])

end

 

修改 app\controllers\edition_controller.rb

def show

    @edition = Edition.find(params[:id])

end

 

修改 app\controllers\composer_controller.rb

def show

    @composer = Composer.find(params[:id])

end

 

8. 创建作品、版本及作曲者的视图

(1)将 app\views\work\show.html.erb

<p>Available editions of

 <%= @work.title %> by

 <%= "#{@work.composer.first_name} #{@work.composer.last_name}" %>

</p>

<table>

  <tr>

    <th>Edition</th>

    <th>Publisher</th>

  </tr>

  <% @work.editions.each do |ed| %>

  <tr>

    <td><%= link_to ed.description || "(no descr.)",

               :controller => "edition",

               :action     => "show",

               :id         => ed.id %></td>

    <td><%= ed.publisher %></td>

  </tr>

  <% end %>

</table>

 

(2)将 app\views\edition\show.html.erb

<% @page_title =

   "#{@edition.work.title} (#{@edition.description})" %>

<p>Details of <%= @edition.work.title %>

(<%= @edition.description %>),

by

<%= "#{@edition.work.composer.first_name}

   #{@edition.work.composer.last_name}" %></p>

<table border="1">

  <tr>

    <th>Publisher</th>

    <th>Year</th>

    <th>Price</th>

  </tr>

  <tr>

    <td><%= @edition.publisher %></td>

    <td><%= @edition.year %></td>

    <td>$<%= @edition.price %></td>

  </tr>

</table>

 

(3)将 app\views\composer\show.html.erb

<% @page_title =

   "Works by #{@composer.first_name} #{@composer.last_name}" %>

<p>Click on any work to see all available editions of that work.</p>

<ul>

  <% @composer.works.each do |work| %>

    <li><%= link_to work.title,

                 :controller => "work",

                 :action     => "show",

                 :id         => work.id %>

    </li>

  <% end %>

</ul>

 

8. 最后,重新启动 WEBrick 服务器,第一个 R4RMusic 应用完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值