加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 具有自定义包装的simple_form自定义输入

发布时间:2020-12-16 19:36:32 所属栏目:百科 来源:网络整理
导读:我想在我的应用程序中为货币设置自定义输入.我有那些bootstrap包装器等(我认为它与simple_form或bootstrap宝石…),所以,我可以做一些像: %= f.input :cost,wrapper = :append do % %= content_tag :span,"$",class: "add-on" % %= f.number_field :cost %%
我想在我的应用程序中为货币设置自定义输入.我有那些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 %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读