Skip to content
This repository

Un wanted multi select #745

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

3 participants

Chris Gunnels Justin French Lyricalpanda
Chris Gunnels

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

and formtastic forces a multi select. How do I stop that?
Justin French

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.

Justin French justinfrench closed this November 21, 2011
Chris Gunnels

that didn't work

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

still gives me a multi select.
Justin French justinfrench reopened this November 21, 2011
Justin French

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

Chris Gunnels

has_and_belongs_to_many

Justin French

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

Justin French

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.

Chris Gunnels

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

Justin French

Ok, closing then.

Justin French justinfrench closed this December 15, 2011
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
Justin French justinfrench reopened this August 14, 2012
Justin French
Owner

Will try to reproduce in a test.

Justin French
Owner

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

Justin French
Owner

Closing due to inactivity.

Justin French justinfrench closed this July 04, 2013
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.