rovnak
2007-Apr-10 18:21 UTC
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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Steve Bartholomew
2007-Apr-10 18:38 UTC
Re: Can one prevent nil from being submitted when ":include_blank => true"?
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
rovnak
2007-Apr-10 18:50 UTC
Re: Can one prevent nil from being submitted when ":include_blank => true"?
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. On Apr 10, 10:38 pm, "Steve Bartholomew" <s...-LWB5c3/XHbdBDgjK7y7TUQ@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Bartholomew
2007-Apr-10 21:45 UTC
Re: Can one prevent nil from being submitted when ":include_blank => true"?
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
rovnak
2007-Apr-10 23:11 UTC
Re: Can one prevent nil from being submitted when ":include_blank => true"?
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'' } %>
<br/>
Country<%= collection_select(
:country_id,:nil , ViewCountry.find(:all), :id, :country,
{ :include_blank => true } ) %>
<br/>
<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'') -%></
a>
<%= loading_indicator_tag(:action => :search) %>
</form>
<script type="text/javascript">
//<![CDATA[
new TextFieldWithExample(''<%= search_input_id %>'',
"Search Terms");
Form.focusFirstElement(''<%= search_form_id -%>'');
//]]>
</script>
On Apr 11, 1:45 am, "Steve Bartholomew"
<s...-LWB5c3/XHbdBDgjK7y7TUQ@public.gmane.org>
wrote:> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
priit.tamboom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-11 04:25 UTC
Re: Can one prevent nil from being submitted when ":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
On Apr 11, 2:21 am, "rovnak"
<donloga...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---