使用ROR连接oracle(或MySQL)数据库
第一步:配置ROR数据库环境
1 安装oci
gem install ruby-oci8 2.0.3 --remote
2 安装oracle-adapter
gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org
rubyonrails是很不错的ROR资源网站,可以下载到大部分的ROR源包。
3 配置我们的数据库环境,在oracle目录下建立我们需要使用的监听。
监听的建立在这里就不讨论了。(本例中使用监听为MSlistener)
4 创建需要的表结构,插入需要的基础数据。(oracle脚本见附件,使用MySQL数据库脚本稍作修改即可)
第二步:开始搭建ROR工程
1 创建oracle工程 rails -d oracle MusicStore(rails -d mysql MusicStore)
2 修改工程 /config/database.yml 配置数据库。
oracle数据库配置:
development:
adapter: oracle
database: MSlistener
username: xxxxx
password: xxxxx
mysql数据库配置:
development:
adapter: mysql
database: MSlistener
username: xxxxx
password: xxxxx
3 启动测试 进入工程目录,启动WEBrick应用服务器 ruby script\server
使用 http://localhost:3000/ 访问页面,如果可以查看工作环境(application’s environment )
则表明数据库已经配置成功。报错则查看工程目录/log/development.log查看原因。
第三步:编写我们的应用
1 创建model ruby script/generate model work;
ruby script/generate model edition;
ruby script/generate model composer;
2 修改model之间的对应关系
打开工程 /app/model 目录
修改 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
3 编写主页面控制器
创建控制器 ruby script/generate controller main welcome
打开工程 编辑 /app/controller/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
4 编写主视图
打开工程 编辑 /app/view/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>
5 编写layout布局
打开工程 在 /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 Music Store</h1> <%= @content_for_layout %> <hr/> <p>Copyright © RubyOnRail, MusicStore</p> </body> </html>
加入默认布局 编辑 /app/controller/application_controller.rb:
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
# helper :all # include all helpers, all the time
#protect_from_forgery # See ActionController::RequestForgeryProtection for details
layout "base"
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
设置应用程序入口 编辑 /config/routes.rb文件:
map.connect '', :controller => "main", :action => "welcome"
6 删除 /pucblic/index.html.启动WEBrick应用服务器 ruby script\server。
ok,现在打开http://localhost:3000/ 就可以看到我们的MusicStore页面了。
第四步:完善我们的应用
1 创建controller ruby script/generate controller work show;
ruby script/generate controller edition show;
ruby script/generate controller composer show;
2 编写控制器
打开工程 /app/controller
修改 work_controller.rb:
class WorkController < ApplicationController
def show
@work = Work.find(params[:id])
end
end
修改 edition_controller.rb:
class EditionController < ApplicationController
def show
@edition = Edition.find(params[:id])
end
end
修改 composer_controller.rb:
class ComposerController < ApplicationController
def show
@composer = Composer.find(params[:id])
end
end
3 编写视图
打开工程 /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>
修改 /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>
修改 /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>
启动WEBrick应用服务器 ruby script\server。
ok,我们的MusicStore程序就基本完成了。
当然,你还可以再加入自己的方法,使应用更加完善!