新手RoR十分钟初体验Step By Step

http://yulimin.javaeye.com/blog/35929

关键字: rails   Ruby Rails RoR    


新手RoR十分钟初体验Step By Step

声明一下,这是个新手启动的体验文章,高手不要看了。。。:)

1、安装Ruby
Windows用户去 http://rubyforge.org/http://rubyforge.org/projects/rubyinstaller/ 下载 One-Click Installer - Windows 安装即可,最新版1.8.5-21

2、测试是否安装成功
建议一个Test.rb文件,内容如下

  1. hello = "Hello Ruby World."  
  2. puts hello  

在命令行下运行 ruby Test.rb ,注意安装时%RUBY_HOME%/bin已经设置在你的PATH中了,检查一下即可,运行正确的话就会显示
Hello Ruby World.
结果出来

也可以启动FreeRIDE,新建一个文件,保存,按F5就可以运行得到结果了。在FreeRIDE的Edit菜单==>Preferences...==>Debugger/Run的右边,选中Run process in terminal,运行时可以打开新的窗口来查看,默认在FreeRIDE下面显示结果。

3、利用gem安装其它模块
在命令行下运行 gem install rails
安装提示与结果
Install required dependency activesupport? [Yn]  y
Install required dependency activerecord? [Yn]  y
Install required dependency actionpack? [Yn]  y
Install required dependency actionmailer? [Yn]  y
Install required dependency actionwebservice? [Yn]  y
Successfully installed rails-1.1.6
Successfully installed activesupport-1.3.1
Successfully installed activerecord-1.14.4
Successfully installed actionpack-1.12.5
Successfully installed actionmailer-1.2.5
Successfully installed actionwebservice-1.1.6
Installing ri documentation for activesupport-1.3.1...
Installing ri documentation for activerecord-1.14.4...
Installing ri documentation for actionpack-1.12.5...
Installing ri documentation for actionmailer-1.2.5...
Installing ri documentation for actionwebservice-1.1.6...
Installing RDoc documentation for activesupport-1.3.1...
Installing RDoc documentation for activerecord-1.14.4...
Installing RDoc documentation for actionpack-1.12.5...
Installing RDoc documentation for actionmailer-1.2.5...
Installing RDoc documentation for actionwebservice-1.1.6...

再运行gem list检查一下看是否安装上去了,会有一堆列表显示,新的也在里面,安装前后各gem list一下,就可以对比得到以下结果

4、安装后各模块介绍
actionmailer (1.2.5) 邮件分发与测试模块,类似Java里的JavaMail的东东
actionpack (1.12.5) 类似于Web的MVC中的V与C
actionwebservice (1.1.6) Web Service支持
activerecord (1.14.4) ORM工具,类似Java中的Hibernate之类的
activesupport (1.3.1) Rails框架所依赖的一些基础工具类
rails (1.1.6) Web应用框架Rails包含模板引擎,控制层及ORM

5、默认安装后的模块有
fxri (0.3.3)
    Graphical interface to the RI documentation, with search engine.
fxruby (1.6.1, 1.2.6)
    FXRuby is the Ruby binding to the FOX GUI toolkit.
log4r (1.0.5) 日志库,Java中就象Log4J或Jakarta的Commons-Logging
rake (0.7.1) 类似make的工具,Java中就象Ant或Maven
sources (0.0.1)
    This package provides download sources for remote gem installation
win32-clipboard (0.4.0)
    A package for interacting with the Windows clipboard
win32-dir (0.3.0)
    Extra constants and methods for the Dir class on Windows.
win32-eventlog (0.4.1)
    Interface for the MS Windows Event Log.
win32-file (0.5.2)
    Extra or redefined methods for the File class on Windows.
win32-file-stat (1.2.2)
    A File::Stat class tailored to MS Windows
win32-process (0.4.2)
    Adds fork, wait, wait2, waitpid, waitpid2 and a special kill method
win32-sapi (0.1.3)
    An interface to the MS SAPI (Sound API) library.
win32-sound (0.4.0)
    A package for playing with sound on Windows.
windows-pr (0.5.1)
    Windows functions and constants predefined via Win32API

6、创建Web应用
在命令行下运行 rails rorweb ,会在rorweb目录下创建相应的一堆完整的Web应用的目录结构,然后你应该做的就是写你的程序了。
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  components
      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  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/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/breakpointer
      create  script/console
      create  script/destroy
      create  script/generate
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  script/process/reaper
      create  script/process/spawner
      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/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

7、启动Web应用
进入rorweb目录,运行 ruby script/server 启动之,信息如下
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-11-25 08:47:44] INFO  WEBrick 1.3.1
[2006-11-25 08:47:44] INFO  ruby 1.8.4 (2006-04-14) [i386-mswin32]
[2006-11-25 08:47:44] INFO  WEBrick::HTTPServer#start: pid=2564 port=3000

默认webrick服务器启用了3000端口,访问一下 http://localhost:3000 ,打开后可以看到如下之类的信息。
Welcome aboard
You’re riding the Rails!
About your application’s environment
点击查看:
Ruby version 1.8.4 (i386-mswin32)
RubyGems version 0.9.0
Rails version 1.1.6
Active Record version 1.14.4
Action Pack version 1.12.5
Action Web Service version 1.1.6
Action Mailer version 1.2.5
Active Support version 1.3.1
Application root X:/OpenSource/Ruby/rorweb
Environment development
Database adapter mysql

关于webrick服务器更多的信息可以访问 http://www.webrick.org/

8、写一个HelloRoR的Web程序
打开rorweb/app目录,可以看到四个子目录
controllers
helpers
models
views
一看就知道是什么作用了吧:)

仍在rorweb目录,运行 ruby script/generate controller HelloRoR 可以看到如下的创建信息
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/hello_ro_r
      exists  test/functional/
      create  app/controllers/hello_ro_r_controller.rb
      create  test/functional/hello_ro_r_controller_test.rb
      create  app/helpers/hello_ro_r_helper.rb

创建一个app/views/hello_ro_r目录及三个文件

打开 hello_ro_r_controller.rb 进行编辑,增加内容后全部如下:

ruby 代码 

  1. class HelloRoRController < ApplicationController   
  2.  def index   
  3.   render_text "Hello RoR World"  
  4.  end  
  5.  def rails   
  6.   render_text "Hello Rails"  
  7.  end  
  8. end  

访问 http://localhost:3000/hello_ro_r/ 就可以看到Hello RoR World结果了
访问 http://localhost:3000/hello_ro_r/rails就可以看到Hello Rails结果了

注意是 hello_ro_r 不是HelloRoR,默认是小写,并写大小写之间用_分隔了,要注意一下,如果要进行对应,就全部小写建立应用,如:
运行 ruby script/generate controller helloror ,这样就是生成 helloror_controller.rb 了,然后访问 http://localhost:3000/helloror/

其它:1.8.4与1.8.5的区别
1.8.4
win32-clipboard (0.4.0)
win32-eventlog (0.4.1)
win32-process (0.4.2)
windows-pr (0.5.1)
1.8.5
win32-clipboard (0.4.1)
win32-eventlog (0.4.2)
win32-process (0.5.1)
windows-pr (0.5.3, 0.5.1)

1.8.5的升级:gem install rails
Bulk updating Gem source index for: http://gems.rubyforge.org
Install required dependency activesupport? [Yn]  y
Install required dependency activerecord? [Yn]  y
Install required dependency actionpack? [Yn]  y
Install required dependency actionmailer? [Yn]  y
Install required dependency actionwebservice? [Yn]  y
Successfully installed rails-1.1.6
Successfully installed activesupport-1.3.1
Successfully installed activerecord-1.14.4
Successfully installed actionpack-1.12.5
Successfully installed actionmailer-1.2.5
Successfully installed actionwebservice-1.1.6
Installing ri documentation for activesupport-1.3.1...
While generating documentation for activesupport-1.3.1
... MESSAGE:   Unhandled special: Special: type=17, text="<!-- HI -->"
... RDOC args: --ri --op F:/OpenSource/RoR/Ruby/lib/ruby/gems/1.8/doc/activesupport-1.3.1/ri --quiet lib
(continuing with the rest of the installation)
Installing ri documentation for activerecord-1.14.4...
Installing ri documentation for actionpack-1.12.5...
While generating documentation for actionpack-1.12.5
... MESSAGE:   Unhandled special: Special: type=17, text="<!-- The header part of this layout -->"
... RDOC args: --ri --op F:/OpenSource/RoR/Ruby/lib/ruby/gems/1.8/doc/actionpack-1.12.5/ri --quiet lib
(continuing with the rest of the installation)
Installing ri documentation for actionmailer-1.2.5...
Installing ri documentation for actionwebservice-1.1.6...
Installing RDoc documentation for activesupport-1.3.1...
Installing RDoc documentation for activerecord-1.14.4...
Installing RDoc documentation for actionpack-1.12.5...
Installing RDoc documentation for actionmailer-1.2.5...
Installing RDoc documentation for actionwebservice-1.1.6...

9、中文截取问题测试
http://my.opera.com/sawpad/blog/show.dml/235183
从以上地址拿到程序放到 rorweb/lib/ 目录下

它的示例里有问题,我回复了它,可以在程序里调用如下,凑合着用,至少不会乱码:

ruby 代码

 

 

  1. u = USCutter.new("GBK")   
  2. u1 = USCutter.new("GBK")   
  3. u2 = USCutter.new("GBK")   
  4. s = "we love 中文截取问题"  
  5. s1 = u.cut(s,0,10)   
  6. s2 = u1.cut(s,0,11)   
  7. s3 = u2.cut(s,0,12)   
  8. puts s1   
  9. puts s2   
  10. puts s3  

记得JavaEye上线时也是乱码问题,现在解决了的,原来我给Robbin写过用JavaScript来解决乱码的问题,不知道Robbin后来如何解决的,搜了一下,没有发现解决方案?

10、接下来就是连接数据库做一个完整的开发之类的,待续。。。

 

时间: 2024-09-02 18:21:09

新手RoR十分钟初体验Step By Step的相关文章

DockOne微信分享(七十六):容器化ICT融合初体验

本文讲的是DockOne微信分享(七十六):容器化ICT融合初体验[编者的话]本次将分享的容器化ICT融合平台是一种面向未来ICT系统的新型云计算PaaS平台,它基于容器这一轻量级的虚拟化技术以及自动化的"微服务"管理架构,能够有效支撑应用快速上线和自动扩缩容,最大化IT基础设施资源利用率并降低总体拥有成本(TCO).未来的网络正在向IT化.云化方向发展,容器与微服务技术,完美契合"网络即服务".网络切片等发展理念,将有助于实现更加灵活.智能.高效和开放的5G新型网

php+jquery+ajax初体验

这个一个简单的php与ajax案例 仅供新手学习 附上效果图 jquery ajax代码  $(function(){   $("#send").click(function(){    var cont = $("input").serialize();    $.ajax({     url:'aseoe.php',     type:'post',     dataType:'json',     data:cont,     success:function

一名UX设计师的VR设计初体验

  本文是某VR团队设计总监在这个新领域当中进行设计初体验时所得到的一些经验分享.相信如今有很多设计师一方面不具备相关的经验,一方面又希望进入VR领域去扩展技能.开拓视野.所以希望本文能够为各位带来一些指引与帮助. 恩,这次仍是VR探索,从设计师职能出发的实战经验漫谈;对比之前的几篇,慢慢可以发现如今的设计先行者们所汇总的东西当中会有一些重要的交集.在尚未实际进入领域之前,通过识别这些交集,我们或许也可以摸到一些关键的东西了.我个人目前还没有能力去做任何偏技术的文章,例如3D方面;在当前阶段,对

一个交互新人的交互设计初体验

  交互设计初体验系列是我在从事交互设计半年多的时间里在工作中和自我学习中积累的一些关于它的思考和经验,也是对自己今年工作上的获得的成长和收获的一个小结.适合初级的交互设计师和对交互设计感兴趣的同学阅读,很多欠缺的地方希望能和大家多交流. 交互设计师这个职业似乎是这几年才开始在中国的一些公司慢慢出现,正处在一个发展的阶段.之前也很少听到有关于这个职业的介绍和培训.而这两年随着"用户体验"越来越多的被运用到产品的设计和商业的发展中,交互设计也在被更多的公司和企业所重视,虽然依然有很多的老

《全栈性能测试修炼宝典 JMeter实战》—第2章 2.1节性能测试初体验

第2章 性能测试初体验 全栈性能测试修炼宝典 JMeter实战 从本章你可以学到: 性能测试的价值 性能测试流程 性能测试成功与失败要素 不同角色看性能 性能测试工具选择 性能测试相关术语 性能测试通过标准 性能测试趋势 性能测试是一项综合性的工作,致力于暴露性能问题,评估系统性能趋势.性能测试工作实质上是利用工具去模拟大量用户操作来验证系统能够承受的负载情况,找出潜在的性能问题,分析并解决:找出系统性能变化趋势,为后续的扩展提供参考.测试显然不是录制脚本那么简单的事情(而且现在很多系统还无法录

Docker的Windows容器初体验

系列文章 第一篇 Docker的Windows容器初体验 - 本文 第二篇 阿里云Windows Server 2016环境Docker试用 最近微软发布了Windows Server 2016,其中最让广大开发者和运维同学期待的就是Docker对Windows容器的支持. Windows支持两种不同类型的容器,即 Windows Server 容器和 Hyper V 容器. 这两种类型的容器的使用方式相同,也支持相同的容器映像. 但是它们的实现机制不同,提供了不同的安全隔离级别 Windows

ASP.NET MVC 3 Beta初体验之超酷的Chart

前面一篇文章:ASP.NET MVC 3 Beta初体验之WebGrid介绍了WebGrid控件的使用,ASP.NET MVC 3 Beta中才内置Chart的.这篇文章中将介绍Chart的使用.包括Chart数据源的配置.Chart的显示.Chart保存三个方面.Chart是很多系统中使用, 所以在ASP.NET MVC 3 Beta初体验之中介绍它是很有必要的. 1.配置Chart的数据源 给Chart 配置数据源大概有三种方式. 第一种:使用数组示例:Controller代码: publi

《张成功项目管理记(第2版)》一第二章 项目管理之初体验

第二章 项目管理之初体验 张成功项目管理记(第2版) 首次与IT项目管理知识体系亲密接触 下午一上班,张成功就迫不及待地敲开了资深项目经理李全才办公室的门.还没等张成功开口,李全才就笑着说:"我听公司领导说,你被委任为国内某省全省大集中电子政务行政办公系统建设项目的项目经理,恭喜你啊.我知道,你今天来找我,一定是想咨询如何管理这个项目,对吧?" 张成功点了点头,李全才示意张成功坐在他办公桌前的座椅上. 令张成功万万没有想到的是,只见李全才从抽屉里拿出一本装订略显粗糙的书(张成功定睛一看

《星际争霸2》人工智能研究环境 SC2LE 初体验

1 前言 2017年8月10号,DeepMind联合暴雪发布了星际争霸2人工智能研究环境SC2LE,从而使人工智能的研究进入到一个全新的阶段.这次,研究人工智能的小伙伴们可以边玩游戏边做研究了. 为了让更多的朋友了解SC2LE研究环境,我们在第一时间对其进行安装测试,并对DeepMind发布的pysc2代码进行分析,初步了解基于pysc2的RL开发方法.下面我们将一一进行介绍. 2 测试使用设备 Macbook Pro 13inch (MacOS Sierra) Alienware 13inch