Web Development 的學習之旅
顯示具有 ruby 標籤的文章。 顯示所有文章
顯示具有 ruby 標籤的文章。 顯示所有文章

2007/06/28

Ruby & Rails 常用的命令列

[更新中]


xx@xx.xx [~/path]# ruby -cw [filename.rb] <-- -c=check, -w=warning
xx@xx.xx [~/path]# ruby -v <-- version
xx@xx.xx [~/path]# ruby -e 'puts "Code demo!"' <-- -e=execute code
xx@xx.xx [~/path]# irb
xx@xx.xx [~/path]# ruby -r profile [filename.rb]
xx@xx.xx [~/path]# ri
xx@xx.xx [~/path]# erb
xx@xx.xx [~/path]# rails /path/to/your/app
xx@xx.xx [~/path]# ruby script/generate model [model_name]
xx@xx.xx [~/path]# ruby script/generate controller [controller_name] [method_name]
xx@xx.xx [~/path]# ruby script/server
xx@xx.xx [~/path]# ruby script/console

2007/06/27

Code Spot

底下的 code 摘自 Ruby for Rails:

class Customer < ActiveRecord::Base
def nice_name
title + " " + first_name + " " +
(if middle_initial then middle_initial + ". " else "" end) +
last_name
end
end

粗體字的部份是一段 if 語法會傳回一個字串, 所以意思就是若 middle_initial 不是空字串時則傳回 middle_initial+". ", 否則傳回一個空字串. 整個判斷語法被當成一個字串使用, 不需要像一般的 language 這樣寫:
def nice_name
if middle_initial then m=middle_initial+". " else m=""
title + " " + first_name + " " + m + last_name
end

這種用法在適當的時候可以讓 code 做更好的呈現, 我覺得更適合人類閱讀.

2007/06/20

Crossing Border(跨越邊界)系列文章

Beyond Java 的作者 Bruce Tate 在 IBM dW 裡有一個 Crossing Borders系列的文章, 寫得非常不錯. 雖然被歸類在 Java technology 裡, 但其實裡面的主角是 Ruby & Rails. 可惜沒有繁體版本, 不過幸運的是至少還有簡體版, 簡體版整體上的翻譯水準不錯, 但專業術語用對岸的話來翻譯, 在閱讀時還是覺得生硬呀.

這系列文章針對許多重要技術關鍵點都有深入的說明, 即使沒有直接使用 Ruby or Rails, 也能在裡面獲得許多很好的概念, 進而更瞭解 Ruby & Rails.

不過目前每篇文章我都只是概略地瀏覽一遍(這是個人的閱讀習慣), 待日後再找機會深入研讀.

2007/06/16

莫名的 require 問題

為了在 rails 使用全文檢索, 因而使用了"雪貂" -> ferret & acts_as_ferret.

由於不能用 svn, 所以無法以 script/plugin 安裝 acts_as_ferret, 只好以 gem 安裝.
在 gem 安裝完畢後, 結果發生了在 environment.rb 裡 require 找不到 acts_as_ferret 的問題.

$GEM_HOME, $GEM_PATH 設定檢查過了, 連 .bashrc 檔也檢查過, Google 大神也拜了, 應該沒問題才對...但問題還是存在. 在絕望時想想乾脆重來算了, 將一些自己安裝的 gem 全部 uninstall, 重新安裝. 安裝一個就測試一次, 結果發現 require 檔案找不到的問題不見了.

問題來得莫名, 消失的也莫名, 浪費了不少時間.

2007/06/13

Rails 讀取 Gem 需要的設定

問題: 在安裝 RedCloth 後, rails 仍無法使用.

即使執行 xxx@xxx.xxx [~/path]# gem install GEM_NAME成功後, rails 仍然無法使用, 解決的方式是在 ./config/environment.rb 內增加下面設定, 告訴 rails 那裡可以找到 gems:

ENV['GEM_PATH'] = '/home/[account_name]/.gems:/usr/lib/ruby/gems/1.8'


下面 HM 裡的文章可以參考之, 不過目前我還沒有更改 .bashrc 檔案, 也沒有 install local gem directory, 只改了 environment.rb 後 rails 就讀得到了.

update: 後來似乎還是有問題, 最後我還是改了 .bashrc 檔案

==========

How do I install my own Gems?

Problem:
The easiest and fastest way to install Ruby gems is to install them in your local directory, you will need shell access. To install your own gems use the following steps:

Solution:
1) Using File Manager in your cPanel make a copy of the .bashrc file in your root directory, name it .bashrc.bak.
2) Now edit the .bashrc file and add the following to the end of the file:
export PATH="$PATH:$HOME/packages/bin:$HOME/.gems/bin"
export GEM_HOME="$HOME/.gems"
export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8"
export GEM_CACHE="$GEM_HOME/cache"


3) Using your favorite SSH client connect to your site.

4) at the prompt type:

xxx@xxx.xxx [~/path]# cp /usr/lib/ruby/gems/1.8/cache/sources-0.0.1.gem ./
xxx@xxx.xxx [~/path]# gem install sources-0.0.1.gem
xxx@xxx.xxx [~/path]# gem update -y

This will update rails to the latest version and install it to your local gem directory.


5) When using a rails application, make sure you add the following to your ./config/environment.rb:
ENV['GEM_PATH'] = '/path/to/your/home/.gems:/usr/lib/ruby/gems/1.8'

2007/06/11

Coding work

在進行 MVC 方面的 coding 時, 經常性會接觸到的就是 \app 及 \test 兩個資料夾內的檔案, 當進行某個 model (例如: Author class)及 Controller(例如: admin/author)相關的作業時, 由於 Rails 預先建好的資料夾目錄, 相關檔案分別會在:

\app\model
\app\controllers\admin
\app\views\admin\author
\test
\test\functional\admin\author

產生的各種檔案可能超過十個以上, 在不同的資料夾切換開啟檔案, 實在沒有效率. 使用 SciTE 的 Session 功能將相關檔案清單儲存起來(ex: Session_Author.ses), 再接照自己的喜好調整開啟的順序. 這樣在修改 Author model 時, 就比較輕鬆了.

接著使用 BC, 建立一個獨立的 Session, 將不須用到的 folder 列在排除的資料夾內, 畫面就清爽許多. 要找出特定檔案 FTP 上傳, 也就容易許多.

2007/06/06

目前版本

下面是目前使用的版本:

xxx@xxx.xxx [~/path]# ruby -v
ruby 1.8.5 (2006-12-04 patchlevel 2) [x86_64-linux]
xxx@xxx.xxx [~/path]# gem -v
0.9.2
xxx@xxx.xxx [~/path]# rails -v
Rails 1.2.3

底下使用 script/about 來顯示:
xxx@xxxx.xxx [~/path]# script/about
About your application's environment
Ruby version 1.8.5 (x86_64-linux)
RubyGems version 0.9.2
Rails version 1.2.3
Active Record version 1.15.3
Action Pack version 1.13.3
Action Web Service version 1.2.3
Action Mailer version 1.3.3
Active Support version 1.4.2
Application root /home/(...)
Environment development
Database adapter mysql