简体   繁体   中英

How can I create wrapper for the types in the simple_form gem?

I want to create a wrapper for my app, but html for different types should be different. So, I can write just

= simple_for_for @task, wrapper: :mine_special do |f|
  = f.input :first_attr, type: :sring
  = f.input :second_attr, type: :boolean

so, my initializer will contain definitions for both string & boolean types:

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

How can I do that?

In short

The best solution I can think of is just naming your wrappers differently:

config.wrappers :mine_special_boolean
config.wrappers :mine_special_string

It follows simple_form semantics nicely and also eliminates the need to remember to pass type option.

Explanation

By the time you are passing wrapper: :mine_special option in the template, simple form initializers code has already been run, so you cannot simply insert an if statement in wrappers block. When you call the wrapper from template, it merges your options with the ones you defined in initializers.

I can imagine a way to override some methods in SimpleForm::FormBuilder to route your wrapper calls based on type option you pass in, but at that time I'll probably be wondering if it's worth the complexity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM