看板 Ruby 關於我們 聯絡資訊
之前曾寫過一篇「Rails: 建立Permalink,避免流水號洩漏網站資料」在板上分享 也一直在Rails 1.2.x用當出的方式去coding.. 後來升級到Rails 2.0之後發現那些URL convention我都用不到 什麼 edit_post_url(@post) 搞得我必須傳 @post.key 搭配Controller中寫的 Find_by_key 後來受不了才發現原來可以在model中用to_param()丟URL的參數進來:p 稍微改寫一下 def find 之後就可以非常優雅地避開使用流水號的問題啦 有興趣的人可以參考文章: http://punk.tw/2008/04/29/857.html 重要程式碼如下(in model) def self.find(*args) if args.first.is_a?(String) find_by_key(args.shift, *args) or raise ActiveRecord::RecordNotFound else super end end def to_param key end -- 至於之前的文章: http://punk.tw/2007/10/06/340.html 我想應該在Rails 1.2.x 也是要用這種方法比較優雅啦~ 突然覺得我土法煉鋼好久,一點都沒有用心深入學好Ruby & Rails orz -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.160.176.165
godfat:我覺得土法煉鋼是必經的程序,呵 04/29 13:01