Un wanted multi select #745

Closed
cgunnels opened this Issue Nov 21, 2011 · 12 comments

3 participants

@cgunnels

I have
```<%= f.input :pages, :as => :select, :collection => pages.flatten %>

and formtastic forces a multi select. How do I stop that?
@justinfrench

Hi, Formtastic makes a guess based on the association type. You can override this guess using the :multiple option, as documented here http://rubydoc.info/gems/formtastic/2.0.2/Formtastic/Inputs/SelectInput:

Override Formtastic's assumption on when you need a multi select
<%= f.input :authors, :as => :select, :input_html => { :multiple => true } %>
<%= f.input :authors, :as => :select, :input_html => { :multiple => false } %>

Closing.

@cgunnels

that didn't work

```<%= f.input :pages, :as => :select, :input_html => { :multiple => false }, :collection => pages.flatten %>

still gives me a multi select.
@justinfrench justinfrench reopened this Nov 21, 2011
@justinfrench

Ok, I'll re-open and check that we have test coverage. What kind of association is :pages?

@cgunnels

has_and_belongs_to_many

@justinfrench

Ok, have reproduced this bug with a test. Looking into it, sorry for the lag in responding.

@justinfrench

Actually, I have a test that "proves" it should work:

  describe 'for a has_and_belongs_to_many association' do
    it 'should allow non-multi select with the :input_html option' do
      output_buffer.replace ''
      concat(semantic_form_for(@freds_post) do |builder|
        concat(builder.input(:authors, :as => :select, :input_html => { :multiple => false }))
      end)
      output_buffer.should_not have_tag('form li select[@multiple="multiple"]')
    end
  end

The logic was a bit smelly, and there weren't any tests for this case:

<%= f.input :pages, :as => :select, :multiple => false, :collection => pages.flatten %>

... so I've added two tests and some code changes to hopefully resolve this. I've committed them in a branch "GH-745", so please bundle in Formtastic from master with the :git option and use the branch name as the :ref option.

@cgunnels Please report back and confirm that this has resolved your problem, then I'll merge it into master.

@cgunnels

This worked:

<%= f.input :pages, :as => :select, :input_html => { :size => 1 }, :multiple => false, :collection => pages.flatten %>

I didn't merge in your changes, I am using version 1.2.4

@justinfrench

Ok, closing then.

@Lyricalpanda

Hey, I just ran into this problem. I'm using formtastic 2.1.1 and tried the following code. My models User and Business are a habtm.

form do |f|
  f.input :users, :as => :select, :input_html => { :size => 1}, :multiple => false, collection: User.where(role:1),    include_blank: false
end

Allowed me to still do a multiple select and this is the generated HTML on the page

<select id="business_user_ids" multiple="multiple" name="business[user_ids][]" size="1"> 

This issue was resolved with a monkey patch as shown on this stackoverflow question (http://stackoverflow.com/questions/11727873/activeadmin-form-not-respecting-multiple-false).

'you could just monkey patch it by placing the following code at config/initializers/formtastic.rb'

module Formtastic
  module Inputs
    class SelectInput
      def multiple_with_options_fix?
        return false if options[:multiple] === false
        multiple_without_options_fix?
      end

      alias_method_chain :multiple?, :options_fix
    end
  end
end
@justinfrench justinfrench reopened this Aug 15, 2012
@justinfrench

Will try to reproduce in a test.

@justinfrench

@Lyricalpanda is your case still a bug if you omit the :size option from your input?

@justinfrench
Owner

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment