hi guys i have a form which has an element, "category id". When i submit it with values, i can see param(:category) with a value (as i print out that value in the controller''s method. Anyway, when i submit my form without selecting any category, the method in the controller does not pick up on it. I tried using .nil? but it doesn''t work. This is my code: def get_subcategories if params[:category] puts " I M in GET SUBCATEGORIES -" + params[:category] + ''...'' @category = Category.find(params[:category]) @sub_categories = @category.sub_categories.all else puts "no given category id to retrieve the subcategory" end end When i run the application and submit without a category value , i notice that params[:category] is still defined (hence printing out "I M in GET SUBCATEGORIES") when it should not be. I have even tried "params[:category].nil?" Any ideas? In perl, i could test the variable for truth (IE if ($params->{''category''}) then ...
Colin Law
2009-Oct-13 12:11 UTC
Re: empty parameter submitted but .nil failed to detect it
2009/10/13 ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > hi guys > > i have a form which has an element, "category id". > > When i submit it with values, i can see param(:category) with a value > (as i print out that value in the controller''s method. > > Anyway, when i submit my form without selecting any category, the > method in the controller does not pick up on it. > I tried using .nil? but it doesn''t work. > > This is my code: > def get_subcategories > if params[:category] > puts " I M in GET SUBCATEGORIES -" + params[:category] + > ''...'' > @category = Category.find(params[:category]) > @sub_categories = @category.sub_categories.all > else > puts "no given category id to retrieve the subcategory" > end > end > > When i run the application and submit without a category value , i > notice that params[:category] > is still defined (hence printing out "I M in GET SUBCATEGORIES") when > it should not be. > I have even tried "params[:category].nil?" >Would .blank? do what you want? Colin
Gregory Mazurek
2009-Oct-13 12:13 UTC
Re: empty parameter submitted but .nil failed to detect it
On Tue, Oct 13, 2009 at 8:11 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/10/13 ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > hi guys > > > > i have a form which has an element, "category id". > > > > When i submit it with values, i can see param(:category) with a value > > (as i print out that value in the controller''s method. > > > > Anyway, when i submit my form without selecting any category, the > > method in the controller does not pick up on it. > > I tried using .nil? but it doesn''t work. > > > > This is my code: > > def get_subcategories > > if params[:category] > > puts " I M in GET SUBCATEGORIES -" + params[:category] + > > ''...'' > > @category = Category.find(params[:category]) > > @sub_categories = @category.sub_categories.all > > else > > puts "no given category id to retrieve the subcategory" > > end > > end > > > > When i run the application and submit without a category value , i > > notice that params[:category] > > is still defined (hence printing out "I M in GET SUBCATEGORIES") when > > it should not be. > > I have even tried "params[:category].nil?" > > > > Would .blank? do what you want? > > Colin > > > Colin''s right - I would code: if not params[:category].blank?-- www.abetaday.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
srinivasan sakthivel
2009-Oct-13 12:15 UTC
Re: empty parameter submitted but .nil failed to detect it
Hi friend, U can try this, if params[:catergory] != nil On Tue, Oct 13, 2009 at 5:41 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/10/13 ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > hi guys > > > > i have a form which has an element, "category id". > > > > When i submit it with values, i can see param(:category) with a value > > (as i print out that value in the controller''s method. > > > > Anyway, when i submit my form without selecting any category, the > > method in the controller does not pick up on it. > > I tried using .nil? but it doesn''t work. > > > > This is my code: > > def get_subcategories > > if params[:category] > > puts " I M in GET SUBCATEGORIES -" + params[:category] + > > ''...'' > > @category = Category.find(params[:category]) > > @sub_categories = @category.sub_categories.all > > else > > puts "no given category id to retrieve the subcategory" > > end > > end > > > > When i run the application and submit without a category value , i > > notice that params[:category] > > is still defined (hence printing out "I M in GET SUBCATEGORIES") when > > it should not be. > > I have even tried "params[:category].nil?" > > > > Would .blank? do what you want? > > Colin > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---