- 前言
 近期在java社区中一种新的脚本语言ruby,及用ruby开发的一个wab框架 rails也热闹了起来.引起了不少的java开发人员的关注.
 本人平时还是很少接触脚本语言方面东东,看到相关的评论
 例如:  "习惯约定优于配置" -- 那样就用象java那样麻烦且繁杂地配置N多XML
 "'一站式'面向用户的简单易用的框架"
 "Ruby 语言- 真正的面向对象脚本语言"
 "Ruby 语法更简单,灵活"
 "声称:Rails 的开发效率是 java 开发的 10 倍"
 等等,再得到老大的强烈推荐,不禁也开始接触了一下.
 
 声明:本人接触三天,看过一些简单的介绍以及一些文档,实操过一个小项目,并未深入了解,这一文章并非语言及框架使用教程,只是一个感性认识,其中代码或许并非最简洁,如要更深入学习ruby,rails,请参考相关的官方站点以及其语法参数书.
 
- Ruby 与 Rails
 [什么是RUBY ?]
 Ruby 是由日本人松本行弘发明的一种面向对象的脚本语言.
 声称,更快,更通俗易懂的面向对象的语言,本人接触这段时间,感觉到关于面向对象的确做得不错.
 一些特点:
 1、一切都是对象,例如:
 数据类型转换提供如 to_ary,to_i,to_s 等方法,
 
 @i = "2".to_i    //字符串转数值类型 @i = "2".to_i    //字符串转数值类型
  puts @i+1 puts @i+1
 
 不再象java中的诸如:
 Integer.parseInt()
 String.valueOf()
 那样难看与及难以理解
 Time.now.strftime(fmt) Time.now对象通过strftime格式化日期类型
 再如koalant在它那本ruby on rails实践中说到   -166.abs 可以直接取其-166绝对值
 ………………
 2、代码块非强制用{} ,无需 ;  作分行标识;
 3、语法灵活,一种可以功能可多种实现,随个人爱好;
 4、语法简洁等,做一个应用只要用到几行代码,这也是脚本语言的优势所在
 
 更详细可参考 http://www.ruby-lang.org/ 以及下面提供的些文档资料.
 
 [什么是Rails ?]
 RAILS是用RUBY开发的一个web框架.
 实现一站式开发.        http://www.rubyonrails.org/ 有比较详细的介绍.
 
 Rails is a full-stack, open-source web framework in Ruby for writing real-world applications with joy and less code than most frameworks spend doing XML sit-ups Rails is a full-stack, open-source web framework in Ruby for writing real-world applications with joy and less code than most frameworks spend doing XML sit-ups
 声称用更少的代码实现web applications。
 此框架主要包括三部分
 Active Record  一个ORM实现
 Action Pack      View 以及controller
 Action Mailer    应该是关于mail方面的东西
 
 
 [一些资料]
 [1] koalant写的Ruby on Rails实践  ,通俗易懂,介绍了ruby on onrails中的一些基础的东东
 [2] IBM中文文章: Ruby on Rails 和 J2EE:两者能否共存?
 [3] Agile Web Development with Rails(以一个案例详细介绍了ruby on rails)
 下载请见:http://www.itpub.net/398528.html
 [4] Ajax on Rails http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html
 [5] Programming Ruby http://www.rubycentral.com/book/index.html
- 快速安装
 1、下载
 到目前为止最新版本是Ruby 1.8.2,可以到http://rubyforge.org/frs/?group_id=167&release_id=2049
 下载 One-Click Installer - for Windows
 2、点击下载exe文件一直NEXT 安装就OK [ 说明:本人安装在C:\ruby\目录]
 3、使用 rubygems 来安装 rails
 CMD进行命令行窗口,通过网络进行远程安装 rails
 说明如果发现gem,rails命令等用不了,可能是环境变量未设置好,
 可自行设置指向你的 安装目录/bin
 
 4、运行服务器
 
 C:\>  ruby c:\myrails\script\server
 => Rails application started on http://0.0.0.0:3000
 => Ctrl-C to shutdown server; call with --help for options
 [2005-08-11 20:29:56] INFO  WEBrick 1.3.1
 [2005-08-11 20:29:56] INFO  ruby 1.8.2 (2004-12-25) [i386-mswin32]
 [2005-08-11 20:29:56] INFO  WEBrick::HTTPServer#start: pid=4072 port=3000
 127.0.0.1 - - [11/Aug/2005:20:30:08 China Standard Time] "GET / HTTP/1.1" 200 32
 34
 - -> /
 127.0.0.1 - - [11/Aug/2005:20:30:09 China Standard Time] "GET / HTTP/1.1" 304 0
 - -> /
 127.0.0.1 - - [11/Aug/2005:20:30:24 China Standard Time] "GET / HTTP/1.1" 304 0
 - -> /
 
 http://127.0.0.1:3000
 恭喜你,如果看到
 Congratulations, you've put Ruby on Rails!页面
 则表示安装成功!!!!
 
 5、 学习一个Ruby的语法
 ruby  以rb为后缀的文件.
 hello.rb
 
 @a = "hello world!" @a = "hello world!"
  puts @a puts @a
 
 C:\ruby\bin>ruby hello.rb
 hello world!
 其它可参考语言书学习.
 
- 我的订餐系统
 
 1、功能,包括四类基本管理,主要是新增,修改,查看,删除(CURD) 四种操作
 餐厅管理(shop)
 菜式管理(good)
 人员管理(human)
 订单管理(order)
 
 2、首页效果截图
                由于本人偷懒,样式借用了jsp wiki的,在此声明一下: )
[主界面,使用frame,左边为主要功能菜单,右边为进行CURD操作提供界面操作]
![orderList[1].gif](/images/blogjava_net/davidxu/orderList[1].gif)
[页面结构如下所示]
![orderList[2].gif](/images/blogjava_net/davidxu/orderList[2].gif)
- 员工管理
 1、快速开发,自动生成基本文件结构目录及文件
 执行命令,自动生成项目所需要的文件及其文件结构,几秒种架设好订餐系统框架!!!
 
 C:\myrails>rails c:\myMealOrder C:\myrails>rails c:\myMealOrder
 
         说明:rails 一直贯穿的思想说是"习惯约定优于配置",所以,文件结构都是它定义的,我们只要将相应
                 文件放到其定义好的目录下就行了。
                  例如:controller 应该放到其 webapp/app/controllers 下,本文目录为[C:\myMealOrder\app\controllers]
                              model 应该放到其      webapp/app/model下,本文目录为[C:\myMealOrder\app\model]
                              views 应该放到其      webapp/app/view下,本文目录为[C:\myMealOrder\app\views]
            另外,根本目录为webapp/public/ 你可以通过修改index.html来定义你自己的index页面
     
          2、启动webrick 服务器测试
                rails默认使用webrick服务器,官方网站了解其更多www.webrick.org
               执行命令
                
 C:\>ruby c:\myMealOrder\script\server
C:\>ruby c:\myMealOrder\script\server  
              打开IE 浏览 :http://127.0.0.1:3000
             看到Congratulations, you've put Ruby on Rails!表示配置成功
   
3、建立数据库以及进行配置 
    CREATE TABLE humen (
    id int(11) NOT NULL auto_increment,
    code varchar(100) NOT NULL default '',
    mame varchar(100) NOT NULL default '',
    PRIMARY KEY  (`id`)
  )       
    说明,习惯约定,默认情况下主键名为id,表名为复数形式,具体操作我也有点糊涂,
    例如本例子中的human model对应其数据表名为humen。这里我就略过了。具体资料可参考更详细的资料。
    配置,打开 C:/myMealOrder/config/database.yml
    将其中参数设置正常就行了。
    例如将 database: rails_development 设成你的数据名等
  4、生成human 的 model,controller文件
  
 ruby c:\myMealOrder\script\generate controller human
 ruby c:\myMealOrder\script\generate controller human
 exists  app/controllers/
      exists  app/controllers/
 exists  app/helpers/
      exists  app/helpers/
 create  app/views/human
      create  app/views/human
 exists  test/functional/
      exists  test/functional/
 create  app/controllers/human_controller.rb
      create  app/controllers/human_controller.rb
 create  test/functional/human_controller_test.rb
      create  test/functional/human_controller_test.rb
 create  app/helpers/human_helper.rb
      create  app/helpers/human_helper.rb 
 ruby c:\myMealOrder\script\generate model human
 ruby c:\myMealOrder\script\generate model human
 exists  app/models/
      exists  app/models/
 exists  test/unit/
      exists  test/unit/
 exists  test/fixtures/
      exists  test/fixtures/
 create  app/models/human.rb
      create  app/models/human.rb
 create  test/unit/human_test.rb
      create  test/unit/human_test.rb
 create  test/fixtures/humen.yml
      create  test/fixtures/humen.yml 
      
     4、快速实现CURD
    打开app/controllers/human_controller.rb文件,修改为
 class HumanController < ApplicationController
   class HumanController < ApplicationController
 scaffold :human
          scaffold :human
 end
  end 
  说明: 简单的一句 "scaffold :human" 就能对应生成human 的CURD操作及其页面。
  重启服务,再次浏览   http://127.0.0.1:3000/human
   action包括:
   list(列表,列出所有human),new(新建human),show(显示human),update(更新human),Destroy(物理删除human),界面如下图



   我们可以注意其URL
   http://127.0.0.1:3000/human/new
   http://127.0.0.1:3000/human/show/1
   http://127.0.0.1:3000/human/edit/1
   http://127.0.0.1:3000/human/destroy/2
   rails也是MVC的模式,
   1、浏览器发出请求;
   2、后台寻找(new/show/edit/destroy/etc) controller,调用相对应的方法,及view模板;
   3、将结果返回给浏览者
   或许你会认为,这一默认的view大多数情况下并不适合,或许需要自己设计view模板,也行,rails提供快捷的方法
   方便用户自己设计view模板,只要定义action并将同名view文件(rhtml后缀的文件)放到其webapp/app/views/目录即可,
   下面将会介绍到相关的操作。
    
- 餐厅及其菜式管理
 餐厅(shop),菜色(good)按员工管理操作方法进行新建,在这里就略过了!!
 http://127.0.0.1:3000/shop
 http://127.0.0.1:3000/good
 
 1、自己设计view 模板
 例如在餐厅列表中我要实现自己的样式,如下图:
  
 具体操作, 新增加 myMealOrder\app\views\shop\list.rhtml (rails中以.rhtml为view模板文件)
 
 <table border="1" width="500px"> <table border="1" width="500px">
  <tr> <tr>
  <td width="20%"><p align="center"><i><b>code</b></i></td> <td width="20%"><p align="center"><i><b>code</b></i></td>
  <td width="20%"><p align="center"><i><b>餐厅名</b></i></td> <td width="20%"><p align="center"><i><b>餐厅名</b></i></td>
  <td width="20%"><p align="center"><i><b>电话</b></i></td> <td width="20%"><p align="center"><i><b>电话</b></i></td>
  <td width="20%"><p align="center"><i><b>备注</b></i></td> <td width="20%"><p align="center"><i><b>备注</b></i></td>
  <td width="10%"><p align="center"><i><b>编辑</b></i></td> <td width="10%"><p align="center"><i><b>编辑</b></i></td>
  <td width="10%"><p align="center"><i><b>删除</b></i></td> <td width="10%"><p align="center"><i><b>删除</b></i></td>
  </tr> </tr>
  <% @shops.each do |shop| %>                  //ruby 语法,循环所有shop <% @shops.each do |shop| %>                  //ruby 语法,循环所有shop
  <tr> <tr>
  <td><%= shop.code %></td> <td><%= shop.code %></td>
  <td><%= link_to shop.name, :action => "show", :id => shop.id %></td> <td><%= link_to shop.name, :action => "show", :id => shop.id %></td>
  <td><%= shop.phone %></td> <td><%= shop.phone %></td>
  <td><%= shop.remark %></td> <td><%= shop.remark %></td>
  <td><%= link_to "编辑", :action => "edit", :id => shop.id %></td> <td><%= link_to "编辑", :action => "edit", :id => shop.id %></td>
  <td><%= link_to "删除", :action => "destroy", :id => shop.id ,:confirm => "Are you sure?" %></td> <td><%= link_to "删除", :action => "destroy", :id => shop.id ,:confirm => "Are you sure?" %></td>
  </tr> </tr>
  <% end %> <% end %>
  </table> </table>
 
 [1]
 <% @shops.each do |shop| %>
 //ruby 语法,循环所有shop
 <% end %>
 
 [2] <%= link_to shop.name, :action => "show", :id => shop.id %>
 生成<a>标签
 
 修改shop_controller.rb
 
 def list def list
  @shops = Shop.find_all @shops = Shop.find_all
  end end
 
 http://127.0.0.1/shop/list 即可发现我们定义的view生效了 :)
 2、进行两表关联
 good 与 shop 关系为多对一的关联
 [1] 要实现在编辑good时可设置其所性shop,先为good表shop_id字段,关联shop表
 
 [2] 修改model文件,具体语法可参见相关的参考书本.这里就略过了
 
 class Good < ActiveRecord::Base class Good < ActiveRecord::Base
  belongs_to :shop belongs_to :shop
  end end
 
 
 class Shop < ActiveRecord::Base class Shop < ActiveRecord::Base
  has_many :goods has_many :goods
  end end
  
 [3] 编辑controller
 
 def edit def edit
  @good = Good.find(@params["id"]) @good = Good.find(@params["id"])
  @shops = Shop.find_all @shops = Shop.find_all
  end end
 [4] 为good 增加view.rhtml
 
 
 <select name="good[shop_id]"> <select name="good[shop_id]">
  <% @shops.each do |shop| %> <% @shops.each do |shop| %>
  <option value="<%= shop.id %>" <option value="<%= shop.id %>"
  <%= ' selected' if shop.id == @good.shop.id %>> <%= ' selected' if shop.id == @good.shop.id %>>
  <%= shop.name %> <%= shop.name %>
  </option> </option>
  <% end %> <% end %>
  </select> </select>
 
 3、正确命名,自动绑定到objcet中
 在java 我们要实现form中的控件提交后自动绑定到object要借用到beanUtil并自己作N多扩展.
 rails中提供了快捷的方法,自要你按要求定义好控件名就能帮自己绑定.
 例如上面的 good[shop_id] 或  good[id] ,good[name] 等等
 
- 订单管理 
 1、order及item关联操作
 
 @orderItem = OrderItem.new() @orderItem = OrderItem.new()
  @human = Human.find(@value) @human = Human.find(@value)
  @good_no_param = "good_" + @value @good_no_param = "good_" + @value
  @good = Good.find(params[@good_no_param]) @good = Good.find(params[@good_no_param])
  @orderItem.human = @human @orderItem.human = @human
 //为item绑定正确的员工及其订的盒饭
  @orderItem.good = @good @orderItem.good = @good
  @orderItem.order = @order @orderItem.order = @order
  @orderItems << @orderItem @orderItems << @orderItem
 //为order绑定新的item
 @order.orderItems = @orderItems
 @order.save  //保存
 
 说明一下,这样操作,修改order 时原item值并没删除或正确的修改,只是将item 的order_id设为0
 另外再增加相应的item.关于这方面的正确用法还有待研究:)
 
- 其它代码简单介绍
 1、日期操作
 
 
 @date = @params["date"] @date = @params["date"]
  fmt = "%Y-%m-%d" fmt = "%Y-%m-%d"
  if @date if @date
  else else
  @date = Time.now.strftime(fmt) @date = Time.now.strftime(fmt)
  end end
 
 2、 find一个对象
 
 Order.find(params[:id]) Order.find(params[:id])
 
 3、条件查询
 
 @shopId = @params["shopId"] @shopId = @params["shopId"]
  @goods = Good.find(:all, @goods = Good.find(:all,
  :conditions => "shop_id = "+@shopId, :conditions => "shop_id = "+@shopId,
  :order => "code desc") :order => "code desc")
 
 
 3、 跳转页面
 
 redirect_to(:action => 'edit') redirect_to(:action => 'edit')
 
 
 4、session应用
 
 session[:good] ||= Good.find(params[:id]) session[:good] ||= Good.find(params[:id])
 
- 问题 
 记录下在开发过程中遇到的问题
 1、没相应的工具支持,操作起来还是比较麻烦的,虽然代码量很少
 2、并不是很健壮,发现挺容易出错,而且出错信息也不全
 3、性能还有待增强,(笔者一边听歌一边开发,发现在运行程序时,不时会影响我的wmplayer播放)
 
- 思考与总结
 本文并非建议大家使用ruby on rails,因为在应用过程中毕竟发现不少的问题.
 然而我们能学习些什么呢??
 [1] 工具支持
 [2] 习惯优于配置
 [3] 一站式的开发
 [4] 更少的代码
 我想在那么大的java 开源社区中实现一站式的开发还是不现实的.
 笔者在想是否能有一个开源项目或者是落实到一个公司的技术框架中能实现到这几点,那样就可以从繁杂的配置以及少了为找N多个开源包或版本的问题而烦恼,而能专心于需求的确认及相关的业务逻辑的改进等等  : )   (完)