演講: Rails2’s template 可以怎麼玩 (HappyDesigner)

Update(2008/3/29): 投影片在此

久違的 HappyDesigner 聚會終於又在辦啦。響應聚會主題“Template, Library, and Framework”,我打算分享的題目是 “Rails2’s template 可以怎麼玩”,內容將會介紹 Ruby on Rails 的 respond_to 機制和各種 template 應用。

  • 時間:2008-03-29 PM 01:30 ~ PM 06:00
  • 地點:新竹Newton’s A蘋果電腦專賣店(地址:新竹市建中一路25號2樓)
  • 官方文宣
  • 報名網頁

FaceboxRender: Rails 無縫使用 lightbox

FaceboxRender 是我最近寫的第二個 Ajax UI plugin,它讓你在 Rails 中可以無縫使用 lightbox 效果,搭配使用的 Javascript library 正如其名是 Facebox。首先你得先裝好 jQuery 和 Facebox:

  1. 下載 jQuery (建議你可以考慮用 jRails 完全換掉 Prototype library)
  2. 下載 facebox
  3. 把 facebox js file 放到 /public/javascripts/
  4. 把 facebox css file 放到 /public/styleshees/
  5. 把 facebox all image files 放到 /public/facebox/
  6. 設定好 layout 的 stylesheet_link_tag 和 javascript_include_tag 加入這些 js,css 檔案

facebox-render 目前放在 github,請下載後放到 vender/plugins/facebox_render

然後在你要使用的 Controller 加入以下程式碼

 include FaceboxRender

或是你直接放到 /app/controllers/application.rb 裡面也是可以。

回到 view code,當你要叫出一個 lightbox :

 facebox_link_to "Login", :url => login_url

或是你也可以用 remote_link_to, remote_form_tag 等Rails內建的 Ajax Helper,差別在於 facebox_link_to 會先秀出一個 Loading lightbox,然後再送 Ajax request。

重頭戲是 action :


def new
 # do some thing you want
 respond_to do |format|
  format.html
  format.js { render_to_facebox }
 end
end

這裡巧妙的運用了 respond_to 來處理 ajax request,預設的 render_facebox 會 render 不帶 layout 的HTML(即 new.html.erb)。當然我們也支援傳入 :action, :template, :partial 等常見的Rails參數,或是乾脆傳入 :html 也可以。

傳入 :msg 的話,會插入一段 <div class=”message”>your msg</div> 到裡面去,方便你做一些提示或警告訊息。

除了 render_facebox,也有 close_facebox 可以關掉 lightbox。也可以用 redirect_from_lightbox 重新讀取另一頁。

實際的應用非常方便,你可以在網頁中用 remote link 或是 remote form submit 叫出 facebox。在 facebox 中也還可以再放 remote link 或 remote form submit,接收處理的 action 可用 render_facebox 或關掉 close_facebox 抑或重新讀取頁面 redirect_from_facebox。

FaceboxRender 的隨附文件請見 README

P.S. 這篇文的英文版在 Handlino’s blog

Rails Front-End 優化

Update(2008/3/24): Yahoo 有份投影片值得一看:

因為 Registrano hosting 在國外的關係,先天速度就慢了一個太平洋。所以如何讓網頁能夠快點 loading 完並且 display 出來變得非常重要且感受明顯。我們可以使用 Firefox 的 YSlow 或 safari web inspector 來檢測實際下載的瓶頸在哪裡。教材則有 O’Reilly High Performance Web Sites 一書值得一讀,裡面的 guideline 有十四點如下:

  1. Make Fewer HTTP Requests
  2. Use a Content Delivery Network
  3. Add an Expires Header
  4. Gzip Components
  5. Put Stylesheets at the Top
  6. Put Scripts at the Bottom
  7. Avoid CSS Expressions
  8. Make JavaScript and CSS External
  9. Reduce DNS Lookups
  10. Minify JavaScript
  11. Avoid Redirects
  12. Remove Duplicates Scripts
  13. Configure ETags
  14. Make Ajax Cacheable

閱讀全文〈Rails Front-End 優化〉

演講: Practical Rails2 (OSDC.tw)

Update(2008/4/13): 今日投影片在此

第一次要在大 conference 上做 presentation,我準備的題目是 Practical Rails2,主要是將這半年內採用 Ruby on Rails 2.0 的經驗做個分享。

RESTful 是 Rails 2.0 的一個大重點,我不打算 cover 太多的 Representational State Transfer 理論 ,而是介紹 Rails2 如何使用 REST 技巧。就算你不是 REST 愛好者或理解其內涵,Rails2 的 RESTful 依然是個非常優秀的技巧,它讓 controller 跟 action 有著統一的思維跟方便的慣例,以及對之後提供 web services 有著極佳的擴充性。

除了REST,接下來還會介紹兩個和多出品的 Ajax UI plugins: SPAkitFaceboxRender,前者讓你可以輕易達成 Single Page Application 的效果,後者則是 lightbox 效果的 Rails 無縫完美使用(using Facebox library)。最後的時間則分享一些好用的 Tips 跟 plugins,像是Capistrano多國語系等等。

內容摘要:

RESTful development is the biggest feature and paradigm shift for Rails2.
I will share our design philosophy and coding experience, including RESTful, Ajax, Metaprogramming, practical tips and useful plugins. This session supposes that you have some Rails background.

講者簡介:

Wen-Tien Chang (a.k.a. ihower) is a software developer and consultant at Handlino Inc., who has been enjoying Ruby on Rails about two years. His recent work has been delvering registrano.com utilizing Rails2, and releasing related open source Rails plugins.

OSDC.tw 2008 的日期為4月12與13,其他的議程見 OSDC.tw 網站,報名請進 Registrano :)

Rails Email 同時提供 plain 及 html 版本

讓 Email 同時提供 text/plain 跟 text/html 版本,讓不支援 HTML 的 Email client 也可以顯示 plain 是一種是具有親和力的作法。根據 multipart-alternative 的定義,正確的格式應該長這樣,用 boundary 分隔成兩個部份:

   From:  Nathaniel Borenstein <nsb@bellcore.com>
   To: Ned Freed <ned@innosoft.com>
   Subject: Formatted text mail
   MIME-Version: 1.0
   Content-Type: multipart/alternative; boundary=boundary42

   --boundary42

   Content-Type: text/plain; charset=utf-8

   ...plain text version of message goes here....

   --boundary42
   Content-Type: text/html; charset=utf-8

   .... html version of same message goes here ...

   --boundary42--

閱讀全文〈Rails Email 同時提供 plain 及 html 版本〉

Rails L10n 多國語言方案

如果是需要多國語言,我在 registrano.com 是使用Globalize plugin,它是把翻譯資料放資料庫,因此還可以做個網站後台來修改,非常方便。語法則是

"Hello, World!".t

我個人是蠻喜歡這種 method call 寫法。不過它也提供與 gettext 一樣的語法:

_("Hello, World!")

下載請前往 Github

另一個更簡單不需要資料庫的 plugin 是 Gibberish,它使用 key-value 方式跟 yml 檔案儲存,語法是

"Hello, World!"[:welcome]

如果不是要多國,而只是想要一國(如中文),則可以考慮裝 Localization SimplifiedLocalization Plugin,前者可以處理 Rails 預設的錯誤訊息跟日期格式等。後者則是因為我自己有個需求是 RHTML view code 可以用中文沒關係,但是我在 controller 或 model 裡蠻不想打中文的(一來是我不知道客戶要的確切文字,也不想讓人家直接去改 controller。二來是 Textmate 不支援中文),所以需要一個最簡單的 L10n 機制來幫忙。

採用 Localization Simplified 後,發現還是碰到 Model attribute name 無法順利中文化,例如出現 “Password 不能是空白” 這樣的錯誤訊息。目前想到這樣 hacking:

#put this in /config/environment.rb
class String
  def humanize
      _( self.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize )
  end
end

於是就可以跟 Localization Plugin 接軌了 :>