undefined method merge_wrapper_options
Asked Answered
G

1

2

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

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

Please help me solve this.

Thanks.

Gangrene answered 10/10, 2014 at 15:39 Comment(4)
Have you created the FakeInput class?Withstand
Yes I've created it.Gangrene
same problem... rails 4.0.3 and I tried simple_form 3.0, 3.0.1, 3.0.2.Doubt
What version of SimpleForm do you use? The document has updated and sample code in the page does not work for older versions of SimpleForm anymore. Please refer wiki page history or use code in this SO answer: https://mcmap.net/q/243682/-rails-simple_form-fields-not-related-to-the-model @JonKernMicron
C
3

Summary

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

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

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

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

Code

/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' }

The POST request will contain:

Parameters: {"utf8"=>"✓", "some_parameter"=>"some-value" }
Caviar answered 12/10, 2014 at 23:54 Comment(1)
You are a lifesaver. Can confirm this works in simple_form 2.1 on Rails3.1. You can also make the field a checkbox by doing = f.input :all_specializations, label: "Select All", as: :fake_string, input_html: { value: nil, type: "checkbox"} To target this attribute in javascript this form attribute will have an id based on the symbol you pass it, so in my example the html attribute to target is id="all_specializations"Parks

© 2022 - 2024 — McMap. All rights reserved.