caa*_*os0 12 ruby-on-rails simple-form
我正在尝试在我的应用中为货币进行自定义输入.我有那些bootstrap包装等(我认为它带有simple_form或带有bootstrap gem ...),所以,我可以做类似的事情:
<%= f.input :cost, wrapper => :append do %>
<%= content_tag :span, "$", class: "add-on" %>
<%= f.number_field :cost %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
它的工作方式与预期一致.问题是:我在很多地方需要同样的东西,而且我不想复制/粘贴它.
所以,我决定创建一个自定义输入.
到现在为止,我得到了以下代码:
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
Run Code Online (Sandbox Code Playgroud)
但是我遇到了一些错误.看起来b没有像预期的那样,所以,它只是不起作用.
真的可以这样做吗?我找不到任何例子,也不能让它自己工作.
提前致谢.
raf*_*nca 17
那个块变量不存在,你的输入法必须是这样的:
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
Run Code Online (Sandbox Code Playgroud)
现在,您可以在Simple Form初始值设定项中为此自定义输入注册默认包装器:
config.wrapper_mappings = { :currency => :append }
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用:
<%= f.input :cost, :as => :currency %>
Run Code Online (Sandbox Code Playgroud)