# Can one prevent nil from being submitted when ":include\_bla

**URL:** <https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606>\
**Category:** Rails\
**Created:** [April 10, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606 "2007-04-10T20:22:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 10, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/1 "2007-04-10T20:22:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 10, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/2 "2007-04-10T20:38:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 10, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/3 "2007-04-10T20:51:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 10, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/4 "2007-04-10T23:47:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 11, 2007, 1:12am UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/5 "2007-04-11T01:12:30Z")

</div>

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

* * *

\<% href = url\_for(params\_for(:action =\> :update\_table, :escape =\>  
false)) -%\>  
\<%= form\_remote\_tag :url =\> href,  
:method =\> :get,  
:before =\>  
“addActiveScaffoldPageToHistory(’#{href}’, ‘#{params[:controller]}’)”,  
:after =\> “$(’#{loading\_indicator\_id(:action  
=\> :search, :id =\> params[:id])}’).style.visibility = ‘visible’;  
Form.disable(’#{search\_form\_id}’);”,  
:complete =\> “$(’#{loading\_indicator\_id(:action  
=\> :search, :id =\> params[:id])}’).style.visibility = ‘hidden’;  
Form.enable(’#{search\_form\_id}’);”,  
:failure =\>  
“ActiveScaffold.report\_500\_response(’#{active\_scaffold\_id}’)”,  
:update =\> active\_scaffold\_content\_id,  
:html =\> { :href =\> href, :id =\>  
search\_form\_id, :class =\> ‘search’ } %\>

  
Country\<%= collection\_select( :country\_id,:nil , ViewCountry.find(:all), :id, :country, { :include\_blank =\> true } ) %\>   

\<%= submit\_tag \_(‘SEARCH\_BUTTON’), :class =\> “submit” %\>  
\<%= \_(‘RESET\_BUTTON’) -%\>\</  
a\>  
\<%= loading\_indicator\_tag(:action =\> :search) %\>

---

<div class="post-metadata">

**Author:** ![rovnak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rovnak](https://www.ruby-forum.com/u/rovnak)\
**Post date:** [April 11, 2007, 6:26am UTC](https://www.ruby-forum.com/t/can-one-prevent-nil-from-being-submitted-when-include-bla/91606/6 "2007-04-11T06:26:46Z")

</div>

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](http://dev.rubyonrails.org/ticket/7664)

Oki, hope it helps  
Priit
