gpt4 book ai didi

ruby-on-rails - 带有自定义包装器的 simple_form 自定义输入

转载 作者:行者123 更新时间:2023-12-04 00:50:26 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中为货币进行自定义输入。我有那些 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

现在,您可以在 Simple Form 初始值设定项中为这个自定义输入注册一个默认包装器:
config.wrapper_mappings = { :currency => :append }

你可以这样使用:
<%= f.input :cost, :as => :currency %>

关于ruby-on-rails - 带有自定义包装器的 simple_form 自定义输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com