Skip to content

rails convention ruby in rails

xdite edited this page Nov 3, 2012 · 4 revisions

Ruby in Rails

  • 如果要判斷空字串或空集合等,請用 xxx.blank?

    • 不要用 xxx.empty? or xxx==""
  • 承上點的相反情形,如果要判斷字串或集合是否有任意成員,請用 xxx.present?

    • " xxx".present? #=> true
    • ["hi","hello"].present? #=> true
    • [].present? #=> false
  • 關於elsif

    • 使用elsif前先思考,處理的邏輯是否有關連性,若無請分成多個if去處理
    • 若有相關性,但必須使用到elsif,思考一下是否有更magic的方式去處理(ex: http://d.pr/i/xNqr)
  • 禁止使用

Post.where( :id => ids ).each do |post| ...
</code>

請改成兩行

@posts = Post.where( :id => ids )
@posts.each  do |post| ...
  • 注意以下兩種寫法topic_id為nil的狀況,
Topic.find(topic_ids) # error
Topic.find(:all, :conditions => { :id => nil}) # []
  • request.host : 當前 request 的 host name example : "http://localhost/" #=> localhost
  • request.request_uri : 當前 request 的 uri (不含host)example : "http://localhost/posts/1" #=> /post/1
  • request.remote_ip : 當前 request 者的 ip
  • 學習參考用投影片以及 application