未定义的方法 merge_wrapper_options [英] undefined method merge_wrapper_options

查看:42
本文介绍了未定义的方法 merge_wrapper_options的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处记录的简单表单的虚假输入:https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes.

I am trying to use a fake input for Simple form as documented here: https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes.

f.input :address, as: :fake

但是,我收到错误消息# 的未定义方法`merge_wrapper_options'".即使在重新启动 rails 服务器后,我仍然收到此错误.

However, I get an error "undefined method `merge_wrapper_options' for #". I get this error even after restarting the rails server.

请帮我解决这个问题.

谢谢.

推荐答案

总结

实例方法 merge_wrapper_optionsSimpleForm::Inputs::Base 类上定义,但直到 3.1.0.rc1 版本才定义.

Summary

The instance method merge_wrapper_options is defined on the SimpleForm::Inputs::Base class, but not until version 3.1.0.rc1.

这是 3.0.2 版的相关源代码(没有 merge_wrapper_options):

Here's the relevant source code for version 3.0.2 (no merge_wrapper_options):

https://github.com/plataformatec/simple_form/blob/v3.0.2/lib/simple_form/inputs/base.rb

将此与版本 3.1.0.rc1 进行对比:

Contrast this with version 3.1.0.rc1:

https://github.com/plataformatec/simple_form/blob/v3.1.0.rc1/lib/simple_form/inputs/base.rb

因此,如果您使用的是 v3.0.2 或更低版本,则不会拥有它.但是,没什么大不了的,只需自己定义方法:

So if you're at v3.0.2 or prior, you won't have it. But, no big deal, just define the method yourself:

/app/inputs/fake_string_input.rb

class FakeStringInput < SimpleForm::Inputs::StringInput

  # Creates a basic input without reading any value from object
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    template.text_field_tag(attribute_name, nil, merged_input_options)
  end # method

  def merge_wrapper_options(options, wrapper_options)
    if wrapper_options
      options.merge(wrapper_options) do |_, oldval, newval|
        if Array === oldval
          oldval + Array(newval)
        end
      end
    else
      options
    end
  end # method

end # class

/app/views/some_form.html.haml

= f.input :some_parameter,
  label:      false,
  as:         :fake_string,
  input_html: { value: 'some-value' }

POST 请求将包含:

Parameters: {"utf8"=>"✓", "some_parameter"=>"some-value" }

这篇关于未定义的方法 merge_wrapper_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆