Forum: Ruby on Rails Can one prevent nil from being submitted when ":include_bla

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
24f3d4353d35f9b1e9b9de79605b3efc?d=identicon&s=25 rovnak (Guest)
on 2007-04-10 20:22
(Received via mailing list)
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

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
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-04-10 20:38
(Received via mailing list)
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

24f3d4353d35f9b1e9b9de79605b3efc?d=identicon&s=25 rovnak (Guest)
on 2007-04-10 20:51
(Received via mailing list)
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.
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-04-10 23:47
(Received via mailing list)
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?

24f3d4353d35f9b1e9b9de79605b3efc?d=identicon&s=25 rovnak (Guest)
on 2007-04-11 01:12
(Received via mailing list)
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';
                    :complete => "$('#{loading_indicator_id(:action
=> :search, :id => params[:id])}').style.visibility = 'hidden';
                    :failure =>
                    :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 } ) %>

<input type="text" name="search" size="50" value="<%= params[:search] -
%>" class="text-input" id="<%= search_input_id %>"
autocompleted="off" />

  <%= submit_tag _('SEARCH_BUTTON'), :class => "submit" %>
  <a href="javascript:void(0)" class="cancel" onclick="f =
this.up('form'); f.reset(); f.onsubmit();"><%= _('RESET_BUTTON') -%></
  <%= loading_indicator_tag(:action => :search) %>

<script type="text/javascript">
  new TextFieldWithExample('<%= search_input_id %>', "Search Terms");
  Form.focusFirstElement('<%= search_form_id -%>');
9a0a4c2b18c09e54f3b130f57a6e77ee?d=identicon&s=25 (Guest)
on 2007-04-11 06:26
(Received via mailing list)

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| [,]} %>

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

And by the way, upcoming rails 1.2.4 should allow :include_blank =>
'some string'

Oki, hope it helps
This topic is locked and can not be replied to.