Un wanted multi select #745

cgunnels opened this Issue Nov 21, 2011 · 12 comments

3 participants


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

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

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 } %>



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

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




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


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 }))
      output_buffer.should_not have_tag('form li select[@multiple="multiple"]')

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.


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


Ok, closing then.


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

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

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

Will try to reproduce in a test.


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


Closing due to inactivity.

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