Ruby on rails 带有自定义包装器的简单表单自定义输入
我正在尝试在我的应用程序中自定义货币输入。我有那些引导包装等等(我认为它带有简单的_形式或引导宝石…),所以,我可以这样做:Ruby on rails 带有自定义包装器的简单表单自定义输入,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,我正在尝试在我的应用程序中自定义货币输入。我有那些引导包装等等(我认为它带有简单的_形式或引导宝石…),所以,我可以这样做: <%= f.input :cost, wrapper => :append do %> <%= content_tag :span, "$", class: "add-on" %> <%= f.number_field :cost %> <% end %> :追加do%> 它的工作原理与预期
<%= f.input :cost, wrapper => :append do %>
<%= content_tag :span, "$", class: "add-on" %>
<%= f.number_field :cost %>
<% end %>
:追加do%>
它的工作原理与预期一样。问题是:我在很多地方都需要同样的东西,我不想到处复制/粘贴
因此,我决定创建一个自定义输入
到目前为止,我得到了以下代码:
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
class CurrencyInput:append do | b|
#内容标签(:span,$,类:“附加组件”)
b、 文本字段(属性名称、输入html选项)
结束
结束
结束
但是我犯了一些错误。看起来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
<%= f.input :cost, :as => :currency %>
您可以这样使用:
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
<%= f.input :cost, :as => :currency %>
:货币%>
我认为这应该行得通:行得通!但是我真的想在我的自定义输入中做这个包装器,所以当我使用货币输入时,我不必传递包装器选项。可能吗?非常感谢。您可以使用包装器映射选项。看,就是这样!非常感谢Rafael,我不知道按字段类型设置包装器。干杯