我正在尝试在我的应用程序中为货币进行自定义输入。我有那些 bootstrap 包装器等(我认为它带有 simple_form 或 bootstrap gem...),所以,我可以做类似的事情:
<%= f.input :cost, wrapper => :append do %>
<%= content_tag :span, "$", class: "add-on" %>
<%= f.number_field :cost %>
<% end %>
它的工作原理正如预期的那样。问题是:我在很多地方都需要同样的东西,而且我不想到处复制/粘贴它。
因此,我决定创建一个自定义输入。
到目前为止,我得到了以下代码:
class CurrencyInput < SimpleForm::Inputs::Base
def input
input_html_classes.unshift("string currency")
input_html_options[:type] ||= input_type if html5?
@builder.input attribute_name, :wrapper => :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
现在,您可以在简单表单初始值设定项中向此自定义输入注册默认包装器:
config.wrapper_mappings = { :currency => :append }
你可以这样使用:
<%= f.input :cost, :as => :currency %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)