include_blank, :prompt params not working? #446

Closed
mlooney opened this Issue Nov 17, 2010 · 17 comments

Comments

Projects
None yet
3 participants

mlooney commented Nov 17, 2010

Hi, I'm trying to get formbuilderinput() to include a blank in my multi-select.
rails 3.0.3, formtastic master tip
-semantic_form_for obj do |f|
=f.input :some_field, :as=>:select, :collection=>current_object.some_collection, :include_blank=>true

Does not include a blank entry in the select options. adding :include_blank=>true has no effect. same deal with :prompt

i suspect it's me, but i can't figure out what i might be doing....

Contributor

lardawge commented Nov 17, 2010

Rails 3.0.3 broke some stuff. Formtastic tests fail on Rails 3.0.3... Rails 3.0.1 everything passes... I would stick with 3.0.1 for now. I am having a large number of tests fail in my work app under Rails 3.0.3 that passed previously. Rails team claim to drop in replacement is a stretch.

mlooney commented Nov 17, 2010

thanks for the headsup. i'll sit tight for now.

Contributor

lardawge commented Nov 17, 2010

I should have asked if it was previously working with Rails 3.0.1?

Owner

justinfrench commented Nov 17, 2010

Interesting... I love it when more work gets dumped in my lap! Creating myself a Rails 3.0.3 gemset to play around in the Formtastic specs. Knowing that it definitely worked under Rails 3.0.1 would help a lot.

Under a fresh Rails 3.0.3 gemset (after relaxing test_environment.rb to allow the new rspec 2.1), there are 9 failing specs. None of them appear to relate to the :prompt or :include_blank option, but we obviously need to resolve this fast.

mlooney commented Nov 17, 2010

i'm notable to get it to function on rails 3.0.1 either. i'm beginning to think it's me.

Owner

justinfrench commented Nov 17, 2010

Ok,

  1. what version of Formtastic do you have installed?
  2. Is the bug reproducable when you bundle in formtastic 1.2.1.beta2 instead?
  3. What kind of association is :some_field
  4. Is obj an ActiveRecord object?

mlooney commented Nov 17, 2010

  1. my formtastic gemfile entry:

    10 gem 'formtastic', :git => "http://github.com/justinfrench/formtastic.git"

  2. yes, that's where i started

  3. habtm

  4. yes

Contributor

lardawge commented Nov 17, 2010

Should it be:
=f.input :some_field, :as=>:select, :collection=>current_object.some_collection, :input_html=> { :include_blank=>true }

Also you shouldn't need this at all unless you set Formtastic::SemanticFormBuilder.include_blank_for_select_by_default to false in formtastic.rb.

mlooney commented Nov 17, 2010

i don't think it needs to be in the input html, and i definitely didn't change that default.

i've just tried it in the input_html to no avail. are you guys seeing it working? if so, then it must be me.

Owner

justinfrench commented Nov 17, 2010

habtm inputs are special. A few releases back, has_many and has_and_belongs_to_many associations in the :select input were treated as not allowing/requiring a prompt or blank line. So, to some extent, this is/was by design.

At some point, I was convinced by a user that there was a use case for has_many to have a blank option, and that logic was implemented. The spec coverage is around line 337–353 of select_input_spec.rb.

It appears that this change did not flow through to has_and_belongs_to_many associations, as those same specs are not repeated, and there's even coverage around not including a blank option.

This is probably easily fixed, I'll add some specs and see what goes red.

In the meantime, you can try lardawge's suggestion above.

Owner

justinfrench commented Nov 17, 2010

It's not you, it's an inconsistency in the behaviour.

mlooney commented Nov 17, 2010

if it is indeed special, then this is a doco bug :)

my particular use case is allowing a user to remove a habtm association from something.

Owner

justinfrench commented Nov 17, 2010

Yup, that's the same use case that convinced me on has_many. Like I said, it's an inconsistency we need to address.

Owner

justinfrench commented Nov 22, 2010

Ok, think I found the issue. Looks like it's not really related to habtm associations specifically. I added a heap of coverage to expose what thought was the bug, but it all passed green.

I found a couple of specs that basically said "should ignore the :include_blank option when :multiple => true". Of course, :multiple is true by default for has_many and habtm associations.

So the offending code was here in black and white:
https://github.com/justinfrench/formtastic/blob/1.2.1/lib/formtastic.rb#L822-826

For :multiple selects, we were ensuring :include_blank was false.

I hope I've resolved this with commit b8f1390, so please bundle master into your app, try it out and confirm. If we're good, this fix will be in 1.2.2.

I'll wait to hear from you.

Owner

justinfrench commented Nov 25, 2010

@mlooney, still looking for a review please!

mlooney commented Nov 26, 2010

both include_blank and prompt are now working. Thanks! (r8f75fc7)

Owner

justinfrench commented Nov 26, 2010

thanks, closing.

sterrym pushed a commit to sterrym/formtastic that referenced this issue Apr 26, 2011

Resolves #446 by not forcing :include_blank to be false on multi sele…
…cts.

Also added a heap of extra coverage.

This issue was closed.

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