带有自定义包装器的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 %>
- 设计:无法关闭require_no_authentication?
- puma生产服务器在后台:omniauth记录到stdout失败导致错误
- Ruby On Rails检索mysql数据库数据’a’不同’b’
- ActiveRecord:如何将属性值插入自定义validation错误消息?
- 具有相同角色的用户如何访问具有不同权限的不同post?
- 无效请求:无效的HTTP格式,Rails中的解析失败
- Rails中的高级Feed解析
- 如何选择一天中每小时的最新记录
- 无法使用cache_classes = true为Concern(ActiveSupport :: Concern :: MultipleIncludedBlocks)定义多个“包含”块