[英]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.