[英]How can I create wrapper for the types in the simple_form gem?
我想為我的應用程序創建一個包裝器,但不同類型的html應該是不同的。 所以,我可以寫
= simple_for_for @task, wrapper: :mine_special do |f|
= f.input :first_attr, type: :sring
= f.input :second_attr, type: :boolean
所以,我的初始化程序將包含string
和boolean
類型的定義:
config.wrappers :mine_special, types: [:boolean] do |b|
# configs here for check box
end
config.wrappers :mine_special, types: [:string] do |b|
# configs here for text field
end
我怎樣才能做到這一點?
簡而言之
我能想到的最好的解決方案就是以不同的方式命名包裝器:
config.wrappers :mine_special_boolean
config.wrappers :mine_special_string
它很好地遵循simple_form語義,也消除了記住傳遞type
選項的需要。
說明
當您在模板中傳遞wrapper: :mine_special
選項時,已經運行了簡單的表單初始化代碼,因此您不能簡單地在包裝塊中插入if語句。 從模板調用包裝器時,它會將您的選項與您在初始化程序中定義的選項合並。
我可以想象一種方法來覆蓋SimpleForm :: FormBuilder中的一些方法,根據你傳入的type
選項來路由你的包裝調用,但那時我可能想知道它是否值得復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.