在这里写出我的实践过程,希望对读这本书,做r4rmusic这个例子的朋友有所帮助,这里主要列出了我的实践过程以及我根据我的实践过程中遇到的问题总结的注意事项:
1.从如下地址下载InstantRails-2.0-win.rar :
http://rubyforge.org/frs/?group_id=904
2.下载后把该文件解压到C盘根目录,解压后的路径:
C:/InstantRails-2.0-win/
3.设置环境变量:
在环境变量中增加ruby可执行文件的路径:
C:/InstantRails-2.0-win/ruby/bin
4.生成应用目录
注意:ruby会在当前目录下创建应用目录
C:/>cd ruby
C:/ruby>cd study_app
C:/ruby/study_app>rails r4rmusicl
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/performance/request
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
C:/ruby/study_app>
5.创建数据库以及用户
mysql> create database r4rmusic1_development;
Query OK, 1 row affected (0.05 sec)
mysql> grant all privileges on r4rmusic1_development.* to 'r4r'@'localhost' iden
tified by 'railzrulez';
Query OK, 0 rows affected (0.02 sec)
6.创建数据库表以及插入数据:
(1).将如下脚本保存在一个文件:r4rmusic1.sql中:
USE r4rmusic1_development;
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 composers (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(25),
last_name VARCHAR(25),
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)
);
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,NULL,"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,"ed. R.O'Rails","D. Black Music House", 1995,
39.95);
INSERT INTO editions VALUES (5,2,"Reprint of 1894 ed.", "RubyTunes,
Inc.", 2003, 35.95);
(2).运行脚本
在命令行执行:mysql -u r4r -p <r4rmusic1.sql
执行后会提示你输入用户密码,输入r4r用户的密码:railzrulez,
然后脚本就被执行。
7.创建模型、控制器
注意:要进入应用的顶级目录来创建模型、控制器
C:/ruby>cd study_app
C:/ruby/study_app>cd r4r* (这里我们进入应用的顶级目录来创建控制器、模型)
(1).创建模型
C:/ruby/study_app/r4rmusicl>ruby script/generate model work
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/work.rb
create test/unit/work_test.rb
create test/fixtures/works.yml
create db/migrate
create db/migrate/001_create_works.rb
C:/ruby/study_app/r4rmusicl>ruby script/generate model edition
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/edition.rb
create test/unit/edition_test.rb
create test/fixtures/editions.yml
exists db/migrate
create db/migrate/002_create_editions.rb
C:/ruby/study_app/r4rmusicl>ruby script/generate model composer
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/composer.rb
create test/unit/composer_test.rb
create test/fixtures/composers.yml
exists db/migrate
create db/migrate/003_create_composers.rb
C:/ruby/study_app/r4rmusicl>
(2).创建控制器
C:/ruby/study_app/r4rmusicl>ruby script/generate controller main welcome
exists app/controllers/
exists app/helpers/
create app/views/main
exists test/functional/
create app/controllers/main_controller.rb
create test/functional/main_controller_test.rb
create app/helpers/main_helper.rb
create app/views/main/welcome.html.erb
C:/ruby/study_app/r4rmusicl>
C:/ruby/study_app/r4rmusicl>ruby script/generate controller work show
exists app/controllers/
exists app/helpers/
create app/views/work
exists test/functional/
create app/controllers/work_controller.rb
create test/functional/work_controller_test.rb
create app/helpers/work_helper.rb
create app/views/work/show.html.erb
C:/ruby/study_app/r4rmusicl>ruby script/generate controller edition show
exists app/controllers/
exists app/helpers/
create app/views/edition
exists test/functional/
create app/controllers/edition_controller.rb
create test/functional/edition_controller_test.rb
create app/helpers/edition_helper.rb
create app/views/edition/show.html.erb
C:/ruby/study_app/r4rmusicl>ruby script/generate controller composer show
exists app/controllers/
exists app/helpers/
create app/views/composer
exists test/functional/
create app/controllers/composer_controller.rb
create test/functional/composer_controller_test.rb
create app/helpers/composer_helper.rb
create app/views/composer/show.html.erb
C:/ruby/study_app/r4rmusicl>
8.为应用配置mysql数据库:
在应用的/config/database.yml文件中进行配置:
development:
adapter: mysql
database: r4rmusic1_development
username: r4r
password: railzrulez
9.编写控制器和视图的代码
该示例的代码可以到如下地址下载:
http://d.download.youkuaiyun.com/source/339489
或者搜索:ruby for rails 源码查找下载
10.启动web服务器:WEBbrick
1.首先要在应用的/config/routes.rb中配置路由:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
该文件中有上面的两行,不能删除或者注释,否则页面显示时报错:No route matches
在该文件中增加:
map.connect '',:controller=>"main",:action=>"welcome"
这样建立一个规则,用来恰当的翻译一个空的URL
2.启动WEB服务器WEBbrick
ruby ./script/server [-b domainname][-p prot]
-b和-p是可选的,如果服务器不能正确启动,可以使用这两个选项
启动WEBbrick
注意:启动WEBbrick服务器需要到应用的顶级目录
本例我们这样运行:
C:/ruby>cd study_app
C:/ruby/study_app>cd r4r*
C:/ruby/study_app/r4rmusicl>ruby ./script/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. INT => stop (no restart).
** Mongrel 1.1.2 available at 0.0.0.0:3000
** Use CTRL-C to stop.
3.在浏览器中输入:
http://localhost:3000
到此该例子成功完成。
注意点:
1.需要将MYSQL安装后文件libmySQL.dll的路径(例如:C:/Program Files/MySQL/MySQL Server 5.0/bin)
加入到环境变量,否则页面启动时会报错找不到:libmySQL.dll
2.其实不需安装ruby,只需要下载文件:InstantRails-2.0-win.rar,解压该文件到
c:/下,然后在环境变量中加入:C:/InstantRails-2.0-win/ruby/bin;
3.view的文件名以erb结尾,不是像书上说的那样以rhtml结尾,例如:
welcome.html.erb
4.应用的/config/routes.rb中配置路由时,不能删除或者注释原有的如下
内容:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
否则页面显示时报错:No route matches