我想在我的应用程序中为货币设置自定义输入.我有那些bootstrap包装器等(我认为它与simple_form或bootstrap宝石…),所以,我可以做一些像:
<%= 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 %>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。