舉例一個很常見的狀況,如果要找匿名文章的話,會下

@posts = Post.where("user_id IS NULL")

這可以等義於以下這種寫法,而且也比較推薦這樣寫,因為是使用 ruby 的格式而不像是 sql command 的格式。

@posts = Post.where(:user_id => nil)

 

但是,如果要找的是排除匿名使用者的所有文章呢? 會用到 sql 的 NOT NULL

Post.where("user_id IS NOT NULL")

很可惜我本來想找出 ruby 格式的寫法,但沒找到,除非裝 meta where gem,有興趣使用的可以參考下面這篇文章

http://stackoverflow.com/questions/4252349/rail-3-where-condition-using-not-null

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

2011/06/06更新: 後來看到有 whitespace-tmbundle ,裝完之後只要 cmd+s 就會自動清除行末空白以及將 tab 換成 space 。

whitespace-tmbundle

===

用 textmate 的話,可以開 Find in Project (熱鍵: shift+cmd+f)

先把 Regular expression 這個選項打勾,再到 Find 的欄位輸入 [ \t]+$ ,Replace 欄位保持空白。

先點 Find 按鈕讓 textmate 去找,然後再點 Replace All ,最後當然要記得做 Save All (熱鍵: alt+cmd+s)。

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

假設狀況是要對 users table 的某一筆資料做 validate email 欄位檢查,打開 rails console 後下

user = User.find 1
user.save # => false
user.errors.invalid? :email # => true/false

當然前提是你的 User model 已經有設定好 validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

如果你有在 layout 中使用 CSS cache,會發現有時候 local 開發正常但 deploy 出去畫面卻大跑版。這很可能是因為 CSS 沒寫好使得 rails 幫忙 merge 時造成衝突而跑版,亦或是 css cache 後的名稱與你的 css 檔案重複而造成衝突。

要在 local 解 bug ,得把以下這行設定打開,就會讓 local 端也產生一樣的 css cache 檔案。

在 layout

<%= stylesheet_link_tag "reset.css", "common-use.css", :cache => "cache-application" %>

在 config/environments/development.rb

config.action_controller.perform_caching = true

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

rvm 本身就有提供配套的 openssl ,但是我的 macbook with OSX 10.5 不知為何會有問題,因此改成先用 brew 安裝 openssl ,再在 install ree 時下參數指定目錄。

brew install openssl
rvm package install readline
rvm install ree --with-openssl-dir=/usr/local/Cellar/openssl/0.9.8o --with-readline-dir=$rvm_path/usr

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

最近看到兩個很方便的 Ruby 網站。

http://tryruby.org/
可以直接在這邊嘗試 ruby 指令,不用安裝 ruby 就可以體驗~
不過我自己使用時,有時候結果會跑不出來 ><

http://www.rubular.com/
本人很不擅長寫 regular expression ,這個網站可以直接寫並且有 WYSIWYG 喔!
可以輕易地測試自己寫出來的正規表示式有沒有問題,能不能正確 work 。

2011/06/08 更新:
感謝 hSATAC 提供另一個 regular expression 的好站: http://gskinner.com/RegExr/

文章標籤

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

Rails 3 使用這個 chinese_permalink gem 在 model 內會出現未定義 chinese_permalink method 的情形

解法是自己新增 config/initializers/chinese_permalink.rb 檔案,內容是

ActiveRecord::Base.send :include, ChinesePermalink

以上是我在專案內的寫法,如果你不喜歡新增檔案,那麼直接在 config/environment.rb 內直接加入這行程式,也是可以解決。

 

另外使用 permalink 會有 SEO 分散的問題,所以要記得在頁面的 head tag 內要加上 canonical url 處理

http://blog.othree.net/log/2011/03/03/canonical-url/

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

<%= f.input :brand_id, :collection => @brands, :include_blank => false, :label => "品牌" %>

使用 :collection ,就會自動切換成 select tag ,不用再加 :as => :select 啦。

但是預設會生出一個空白選項,你可以用 :prompt => "預設文字" 設定它,或是像我一樣使用 :include_blank => false 移除這個空白選項。

文章標籤

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()

原本我是使用 handicraft_form ,但因為現在 ihower 沒再更新他了,在同行以及 railscast 建議之下改用 simple_form 。

改進的地方令人驚訝,首先你根本就不用管表單內各欄位要用哪種 form tag ,寫的時候一律用 f.input , simple_form 會根據 column type 去對應產生需要的 form tag ,真的要自訂再用 :as => :wanted_tag 去改。詳細可以參考專案內的 Mappings/Inputs available 。

https://github.com/plataformatec/simple_form
https://github.com/techbang/handicraft_helper

如果你對設定上不滿意,可以在 config/initializers/simple_form.rb 進行你需要的設定。
如果需要 i18n ,可以新增config/locales/simple_form.zh-TW.yml ,內容如下,我自己只有制定基本中文化而已:

zh-TW:
  simple_form:
    "yes": '是'
    "no": '否'
    required:
      text: '必填'
      mark: '*'
      # You can uncomment the line below if you need to overwrite the whole required html.
      # When using html, text and mark won't be used.
      # html: '<abbr title="required">*</abbr>'
    error_notification:
      default_message: "很抱歉,請您再檢查一下:"

使用 simple_form 要注意的是,必須餵給他 @object ,而不能用 symbol 。 simple_form 會自動幫你抓出 model 內設定驗證的欄位,自動加上 * 表示必填,並且如果漏填噴 error_messages 時,自動幫你補在欄位後方。

ps. Rails 3 拿掉了 error_messages, error_messages_on, error_messages_for 等 method ,如果想要使用,必須安裝 dynamic_form 。

https://github.com/rails/dynamic_form

文章標籤

笨笨小蟹 發表在 痞客邦 留言(2) 人氣()

Rails 3 有使用 lazy loading ,因此預設並不會載入 lib/ 目錄(跟rails 2不同),但有進 load_path 所以可以正常的 require 。

想要自動載入可以在 config/application.rb 內加入這行:

config.autoload_paths += %W( #{config.root}/app/lib )

參考文件:

Rails3 自動載入
Rails 3 RC 公告
Commit - the autoload issue
http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3
http://www.hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all-subdirectories/

笨笨小蟹 發表在 痞客邦 留言(0) 人氣()