A simple use case; A page has two drop down values; age_from & age_to If age_to is lesser than age_from , I need to show an error message to the user and go back to the form. When I go back to the form the values selected by the user is lost. How should I handle this case so that the values entered by the user is not lost? The controller is given below class SearchController < ApplicationController def show end def search age_from = params[:search][:age_from] age_to = params[:search][:age_to] if age_from > age_to #validation message to the user render :action => ''show'' end end end _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Francois Beausoleil
2005-Oct-21 04:56 UTC
Re: validation in controller makes loss of selected values
Hi ! 2005/10/20, Neeraj Kumar <neeraj.jsr@gmail.com>:> class SearchController < ApplicationController > > def show > end > > def search > age_from = params[:search][:age_from] > age_to = params[:search][:age_to] > if age_from > age_to > #validation message to the user > render :action => 'show' > end > end > > endReplace the local variables with instance ones, and use that in the view: @age_from = params[:search][:age_form] (view): <%= text_field :search, :age_from, :value => @age_from %> NOTE: Your validation as written can be fooled - you don't even know if you've received valid numbers. You should do @age_from Integer(params[:search][:age_from]), and catch any resulting exception. Hope that helps ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails