Can one prevent nil from being submitted when ":include_blank => true"?

I'm very close to getting some cool functionality with nearly zero extra controller coding ...but one sticking point.

I have a simple select

Country<%= collection_select(       :country_id, nil, ViewCountry.find(:all), :id, :country, :include_blank => true ) %>

I use ":include_blank" so that the list has a blank option. This is all rendering properly.

My complaint is that on the Ajax submit (and maybe this is an Ajax thing), I'm getting Parameters: { "country_id"=>[nil], ...}

But, I really don't want ANY parameter submitted for this select when the user has selected the blank option.

Is there a way to prevent that submit using some combination of select options?

FYI - I'm pretty sure if this was a trad html submit, that no parameter woud be submitted, so maybe this is a peculiarity of the prototype.js?

It's not a prototype thing. A blank option will submit a blank value which Rails converts to nil. You're better of checking for the nil using validation or manually in your action and returning an error message.

Steve

It kind of sucks, because the params in question are being automatically appended to the where clause in a select. In the case of the blank option, I don't want to return an error. Its a perfectly valid choice by user. It means "no where criteria". But because the "nil" value is being propagated, I'm getting stupid stuff like "where city_id = NULL" in the select.

Everything was code free and elegant up to this point. Now I will have to intercept every request check for these edge cases in the params and remove them. All of the elegance just broke down.

How are the params getting appended to the find conditions? Can you post your action's code and the names of any plugins you're be using?

Steve

I'm using ActiveScaffold. It has a collection of Ajax enabled .rhtml templates and helpers which collectively peform a lot of magic. Very powerful but not easy to follow.

Anyway, the List scaffold page includes a Search form with a single text field.. When user submits the search form the text contents are applied to the where clause and the list is regenerated.

I found that by adding additional input fields (selects) to the Search form template, that these additional field values were being appended to the where clause AND nicely propagated to all of the other actions on the scaffold (like column sort and pagination).

But there was one glitch. When I added the ":include_blank => true" to my selects I found that the auto-appended where criteria now included "where city_id = NULL" which obviously makes my select return 0 rows.

Here's an example of the _search.rhtml template with my mods

My only add is

Country<%= collection_select(       :country_id,:nil , ViewCountry.find(:all), :id, :country, { :include_blank => true } ) %>

Hi!

You can add a new custom empty to select this way:

<%= select :country, :country_id, [["my custom empty string", 0]] + ViewCountry.find(:all).collect {|item| [item.name, item.id]} %>

OR you can change nil to 0 from at model (don't like this way, but you still can do it :-):

  before_validation :fix_nil_for_temporary

def fix_nil_for_temporary     if country_id == nil       self.country_id = 0     end   end

And by the way, upcoming rails 1.2.4 should allow :include_blank => 'some string' http://dev.rubyonrails.org/ticket/7664

Oki, hope it helps Priit