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.

user3477051
  • 409
  • 3
  • 6
  • 13

1 Answers1

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" }
admgc
  • 273
  • 3
  • 9
  • 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"` – Kelsey Hannan Mar 16 '15 at 02:12