顯示具有 Ruby 標籤的文章。 顯示所有文章
顯示具有 Ruby 標籤的文章。 顯示所有文章

2014年6月17日 星期二

RoR Hello World

MVC Model in Rails

1. Generator a controller

rails generate controller welcome

      create  app/controllers/welcome_controller.rb
      invoke  erb
      create    app/views/welcome
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      create      test/helpers/welcome_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.js.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.css.scss



2.Add new setting in routing file (config/routes.rb)

get "welcome/say_hello" => "welcome#say"


3. edit controller fileopen app/controllers/welcome_controller.rb, and then ass a say method.

class WelcomeController < ApplicationController 
    def say 
    end
end


4.add app/views/welcome/say.html.erb

<h1>Hello, World!</h1>


以CRUD方式開始RoR應用

CRUD means the four basic type: Create, Read, Update and Delete for data operation.

1.build work space

$ mkdir projects
$ cd projects

2. build new app project

$ rails new blog
$ cd blog
$ ls -al




檔案/目錄用途
Gemfile設定Rails應用程式會使用哪些Gems套件
README專案說明:你可以用來告訴其他人你的應用程式是做什麼用的,如何使用等等。
Rakefile用來載入可以被命令列執行的一些Rake任務
app/放Controllers、Models和Views檔案,接下來的內容主要都在這個目錄。
config/應用程式設定檔、路由規則、資料庫設定等等
config.ru用來啟動應用程式的Rack伺服器設定檔
db/資料庫的結構綱要
doc/用來放你的文件
lib/放一些自定的Module和類別檔案
log/應用程式的Log記錄檔
public/唯一可以在網路上看到的目錄,這是你的圖檔、JavaScript、CSS和其他靜態檔案擺放的地方
script/放rails這個指令和放其他的script指令
test/單元測試、fixtures及整合測試等程式
tmp/暫時性的檔案
vendor/用來放第三方程式碼外掛的目錄

3. Start the server

$ cd projects/demo
$ rails server

4. check the default web page
open browser and then goto http://localhost:3000

2014年6月14日 星期六

Install RoR on Ubuntu 10.04 x86_64


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Install Ruby Using apt-get (Out of date)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1. Install Ruby interpreter

$sudo apt-get install ruby
$ruby -v (make sure the install sucess, ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux])
$sudo apt-get install ruby-dev
$sudo apt-get install libopenssl-ruby (Solution for "no such file to load -- net/https (LoadError)")
2. Install Ruby Package Management program
$sudo apt-get install rubygems
$gem -v (make sure the install sucess 1.3.5)

#speed up by setting no doc files download for each pakage install
$vim ~/.gemrc
insert the content "gem: --no-ri --no-rdoc"

3.Install Rails
$gem install rails

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Install Ruby Using RVM

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$\curl -sSL https://get.rvm.io | bash -s stable --ruby

$ source /home/stanley3/.rvm/scripts/rvm
(or add "source ~/.rvm/scripts/rvm" in the ~/.bashrc)
$ruby -v
(ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux])
$gem -v (2.2.2)

$rvm list known (查看可安裝的 Ruby 實作)

$rvm use ruby_version (can switch between the Ruby versions)

add the following line into .bashrc to load rvm

# This loads RVM into a shell session.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

Install Node.js

Since Rails 3.1, a JavaScript runtime has been needed for development on Ubuntu Linux. The JavaScript runtime is required to compile code for the Rails asset pipeline. For development on Ubuntu Linux it is best to install the Node.js server-side JavaScript environment.

Note: the following green key is from the output of $apt-get update
$sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B9316A7BC7917B12
$sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get install nodejs
$ which node (expected output: /usr/bin/node)


Rails Installation


$gem install rails (the most recent stable release)
$rails -v