带有自定义包装器的simple_form自定义输入

我正在尝试在我的应用中为货币进行自定义输入。 我有那些bootstrap包装器等(我认为它带有simple_form或带有bootstrap gem …),所以,我可以做类似的事情:

 :append do %>    

它的工作方式与预期一致。 问题是:我在很多地方需要同样的东西,而且我不想复制/粘贴它。

所以,我决定创建一个自定义输入。

到现在为止,我得到了以下代码:

 class CurrencyInput  :append do |b| # content_tag(:span, "$", class: "add-on") b.text_field(attribute_name, input_html_options) end end end 

但是我遇到了一些错误。 看起来b没有像预期的那样出现,所以,它只是不起作用。

真的有可能这样做吗? 我找不到任何例子,也不能让它自己工作。

提前致谢。

那个块变量不存在,你的输入法必须是这样的:

 class CurrencyInput < SimpleForm::Inputs::Base def input input_html_classes.unshift("string currency") input_html_options[:type] ||= input_type if html5? template.content_tag(:span, "$", class: "add-on") + @builder.text_field(attribute_name, input_html_options) end end 

现在,您可以在Simple Form初始值设定项中为此自定义输入注册默认包装器:

 config.wrapper_mappings = { :currency => :append } 

您可以像这样使用:

 <%= f.input :cost, :as => :currency %>