Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Default options aren't applied to inputs generated with input_field #712

Closed
smidwap opened this Issue · 3 comments

3 participants

Matt De Leon Rafael Mendonça França Vasiliy Ermolovich
Matt De Leon

I ran into a use case where I needed the default options passed to simple_form_for to apply to fields generated with input_field. For example:

simple_form_for(@user, defaults: { disabled: true }) do |f|
  f.input :first_name
  f.input_field :last_name
end

will only include the "disabled=true" attribute for first_name, but not last_name.

This feels like unexpected behavior. In my application, I am displaying a form either with all inputs disabled or all inputs enabled, and some of my inputs are generated with input_field.

I perused the codebase to figure out why this might be the case, and I found this test in input_field_test.rb:

  test 'builder input_field should not modify the options hash' do
    options = { :id => 'name_input', :class => 'name' }

    with_concat_form_for(@user) do |f|
      f.input_field :name, options
    end

    assert_select 'input.string.name#name_input'
    assert_equal({ :id => 'name_input', :class => 'name' }, options)
  end

Is there a reason input_field should not modify the options hash at all?

I'm happy to add default option behavior to input_field, but I want to first see whether there's an important reason this feature doesn't already exist.

Rafael Mendonça França

This is a bug. If you want to make the patch please go ahead

Rafael Mendonça França

About the options hash, we prefer to not modify the arguments inside the methods. If we modify we can't reuse the same hash object for successive calls.

options = { class: 'foo' }

f.input_fields :name, options
f.label :name, options
Vasiliy Ermolovich
Collaborator

@smidwap thanks for the pull request!

Vasiliy Ermolovich nashby closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.