原本我是使用 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

arrow
arrow
    文章標籤
    handicraft rails github
    全站熱搜

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