I would like to verify a parameter is present before a search is posted. min_age = params[:min_age] max_age = params[:max_age] @users = User.find(:all, :conditions => ["years >= ? AND years <= ? " min_age, max_age]) Basically I am just concerned about the max_age. I tried this: min_age = params[:min_age] max_age = params[:max_age] if max_age.nil? flash[:notice] = "please enter max age!" redirect_to :action => "index" @users = User.find(:all, :conditions => ["years >= ? AND years <= ? " min_age, max_age]) But the search still ran with a nil max_age parameter. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2010 03:07, Sean Six <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I would like to verify a parameter is present before a search is posted. > > min_age = params[:min_age] > max_age = params[:max_age] > > @users = User.find(:all, > :conditions => ["years >= ? AND years <= ? " min_age, > max_age]) > > Basically I am just concerned about the max_age. > > I tried this: > min_age = params[:min_age] > max_age = params[:max_age] > > if max_age.nil? > flash[:notice] = "please enter max age!" > redirect_to :action => "index"You need an else in here Colin> > @users = User.find(:all, > :conditions => ["years >= ? AND years <= ? " min_age, > max_age]) > > > But the search still ran with a nil max_age parameter. > -- > Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2010 03:07, Sean Six <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I tried this: > min_age = params[:min_age] > max_age = params[:max_age] > > if max_age.nil? > flash[:notice] = "please enter max age!"If there is a field called "max_age" on the form, but the user leaves it blank, you won''t get nil from params, you''ll get an empty string. So if you check for max_age.blank? instead, you should get the result you want. (Running a debugger would help you find this out very easily, or even just outputting the value of max_age to the console to see if it''s what you expect it to be...) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.