天天看點

新手RoR十分鐘初體驗Step By Step

<a href="http://yulimin.javaeye.com/blog/35929">http://yulimin.javaeye.com/blog/35929</a>

關鍵字: rails   ruby rails ror    

新手ror十分鐘初體驗step by step

聲明一下,這是個新手啟動的體驗文章,高手不要看了。。。:)

1、安裝ruby

2、測試是否安裝成功

建議一個test.rb檔案,内容如下

hello = "hello ruby world."  

puts hello  

在指令行下運作 ruby test.rb ,注意安裝時%ruby_home%/bin已經設定在你的path中了,檢查一下即可,運作正确的話就會顯示

hello ruby world.

結果出來

也可以啟動freeride,建立一個檔案,儲存,按f5就可以運作得到結果了。在freeride的edit菜單==&gt;preferences...==&gt;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 啟動之,資訊如下

=&gt; booting webrick...

=&gt; rails application started on http://0.0.0.0:3000

=&gt; 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

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 代碼 

class hellororcontroller &lt; applicationcontroller   

 def index   

  render_text "hello ror world"  

 end  

 def rails   

  render_text "hello rails"  

end  

注意是 hello_ro_r 不是helloror,預設是小寫,并寫大小寫之間用_分隔了,要注意一下,如果要進行對應,就全部小寫建立應用,如:

其它:1.8.4與1.8.5的差別

1.8.4

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

while generating documentation for activesupport-1.3.1

... message:   unhandled special: special: type=17, text="&lt;!-- hi --&gt;"

... 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)

while generating documentation for actionpack-1.12.5

... message:   unhandled special: special: type=17, text="&lt;!-- the header part of this layout --&gt;"

... rdoc args: --ri --op f:/opensource/ror/ruby/lib/ruby/gems/1.8/doc/actionpack-1.12.5/ri --quiet lib

9、中文截取問題測試

<a href="http://my.opera.com/sawpad/blog/show.dml/235183" target="_blank">http://my.opera.com/sawpad/blog/show.dml/235183</a>

從以上位址拿到程式放到 rorweb/lib/ 目錄下

它的示例裡有問題,我回複了它,可以在程式裡調用如下,湊合着用,至少不會亂碼:

ruby 代碼

u = uscutter.new("gbk")   

u1 = uscutter.new("gbk")   

u2 = uscutter.new("gbk")   

s = "we love 中文截取問題"  

s1 = u.cut(s,0,10)   

s2 = u1.cut(s,0,11)   

s3 = u2.cut(s,0,12)   

puts s1   

puts s2   

puts s3  

記得javaeye上線時也是亂碼問題,現在解決了的,原來我給robbin寫過用javascript來解決亂碼的問題,不知道robbin後來如何解決的,搜了一下,沒有發現解決方案?

10、接下來就是連接配接資料庫做一個完整的開發之類的,待續。。。