[指令碼_Ruby]第一個Ruby On Rails WEB應用程式HelloWorld

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   strong   檔案   sp   div   

經過不懈努力終於把Ruby On Ralis 的開發環境搭建好了,在WIN7系統下,趕緊來個最最常用的例子來練練手吧,感受一下Ruby。。。

再次聲明一下,我的ruby -v是ruby-2.0.0p247。gem -v是:2.0.3。rails -v 是:4.0.0。應該是比較新的吧!所以一些命令可能不一樣吧!首先先建立一個工作目錄吧!我的是:D:\RubyWorkSpace。

開啟ruby的字元命令介面,進入到工作目錄,建立項目hello.

 

[plain] view plaincopyprint?
  1. rails new hello  

老版本中的是rails hello,在這個新版本中新引入了new.由於國內的原因,種種牆的阻隔,使我們在建立項目的時候可能出現問題。如果不出問題的話,你的項目會在一系列的create之後聽到一行代碼為run bundle install。然後就沒有然後了,不管如何等人家就是不動。這個時候需要在建立項目的時候加入一些參數了。如下:

 

[plain] view plaincopyprint?
  1. rails new hello --skip-bundle  

這就告訴rails建立項目的時候跳過bundle的安裝,這個時候看到結果了吧,這時進入到hello項目中開啟Gemfile檔案,在第一行有個:source ‘https://rubygems.org/‘改成我們國內的鏡像源:

 

[plain] view plaincopyprint?
  1. source ‘http://ruby.taobao.org/‘  

這個時候再安裝bundle就沒有問題了:

 

[plain] view plaincopyprint?
  1. gem install bundle  

這時需要建立個控制器:say具體動作為:hello

 

[plain] view plaincopyprint?
  1. rails generate controller Say hello  

之前版本中都是用ruby script/generate controller產生的,產生成功之後,進入到\hello\app\controllers中開啟say_controller.rb檔案,看到如下所示:

 

[ruby] view plaincopyprint?
  1. class SayController < ApplicationController  
  2.   def hello  
  3.   end  
  4. end  

進入到:\hello\app\views\say中開啟編輯:hello.html.erb

 

[html] view plaincopyprint?
  1. <h1>這是我的第一個Ruby On Rails Web 應用程式!</h1>  
  2. <p>Hello World!</p>  

進入到\hello\config開啟:routes.rb我們將看到項目中的路由選擇:

 

[html] view plaincopyprint?
  1. get "say/hello"  

所以我們可以通過http://localhost:3000/say/hello存取控制器再到頁面。

其他的產生的還有JS,CSS檔案等,這裡就不一一說明了

啟動伺服器:

 

[plain] view plaincopyprint?
  1. rails server  

上個啟動並執行結果:

特別說明下:如果頁面中像這樣出現中文的話,需要將hello.html.erb儲存為UTF-8格式,否則會出錯。

[指令碼_Ruby]第一個Ruby On Rails WEB應用程式HelloWorld

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.