Rinauro Carol
2005-Sep-07 23:22 UTC
Newbie rails user needs validation/error handling help
I have a rails app for our timetracking system and we have one page where the validation out of the box doesn''t seem to work. The controller code is as follows.... def create @timerecord = Timerecord.new(@params[:timerecord]) @session[:last_date]=-mAYBkpmon6te7gF2qHpJQA@public.gmane.org if @timerecord.save flash[''notice''] = ''Timerecord was successfully created.'' if @params[:commit]==''Save and new'' if @session[:working]==1 redirect_to :action => ''new_working'' else redirect_to :action => ''new_non_working'' end else redirect_to :action => ''my_list'' end else if @session[:working]==1 redirect_to :action => ''new_working'' else redirect_to :action => ''new_non_working'' end end end The model code starts like... class Timerecord < ActiveRecord::Base belongs_to :user belongs_to :project belongs_to :activity validates_presence_of :user_id validates_presence_of :date validates_presence_of :project, :message=>"project is required" #validates_presence_of :activity, :message=>"activity is required" validates_presence_of :activity validates_presence_of :hours The view code... <h1>New time record <%= @session[:working]==1 ? "(working)" : "(non-working)" %></h1> <%= error_messages_for ''timerecord'' %> <table> <tr> <td valign="top"> <%= start_form_tag :action => ''create'' %> <%= render_partial "form" %> <%= submit_tag "Save and new"%> <%= submit_tag "Save and return" %> <%= end_form_tag %> <%= link_to ''Back'', :action => ''my_list'' %> </td> <td width="40px"/> <td valign="top"> <%= render_partial "currentdaysummary" %> <br/> <%= render_partial "fiscalmonthsummary" %> </td> </tr> </table> The "validates presence of" doesn''t seem to work at least to provide user feedback. Records missing the activity are not saved but the user is not given any visual feedback either. I have tried other things such as my own validations, rescue code, etc... and the error is getting fired I just can''t seem to redirect the user back to his previous page (with all the data still set on it) and provide them a useful message. This stuff works great for my other forms (which don''t have two submit options - Save and new vs. Save, and two different views of the same form based on the record type, working vs. non working). Any help? Carol A. Rinauro _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Bogdan Ionescu
2005-Sep-07 23:39 UTC
Re: Newbie rails user needs validation/error handling help
Well, I'm kind of a newbie myself, but normally if your save fails you shouldn't redirect your page to some other action, if you want to see the form and the values. So I would remove the else branch. Bogdan On 9/8/05, Rinauro Carol <CarolRinauro@foodorigins.com> wrote:> > I have a rails app for our timetracking system and we have one page where > the validation out of the box doesn't seem to work. > > The controller code is as follows…. > > def create > > @timerecord = Timerecord.new(@params[:timerecord]) > > @session[:last_date]=@timerecord.date > > if @timerecord.save > > flash['notice'] = 'Timerecord was successfully created.' > > if @params[:commit]=='Save and new' > > if @session[:working]==1 > > redirect_to :action => 'new_working' > > else > > redirect_to :action => 'new_non_working' > > end > > else > > redirect_to :action => 'my_list' > > end > > else > > if @session[:working]==1 > > redirect_to :action => 'new_working' > > else > > redirect_to :action => 'new_non_working' > > end > > end > > end > > The model code starts like… > > class Timerecord < ActiveRecord::Base > > belongs_to :user > > belongs_to :project > > belongs_to :activity > > validates_presence_of :user_id > > validates_presence_of :date > > validates_presence_of :project, :message=>"project is required" > > #validates_presence_of :activity, :message=>"activity is required" > > validates_presence_of :activity > > validates_presence_of :hours > > The view code… > > <h1>New time record <%= @session[:working]==1 ? "(working)" : > "(non-working)" %></h1> > > <%= error_messages_for 'timerecord' %> > > <table> > > <tr> > > <td valign="top"> > > <%= start_form_tag :action => 'create' %> > > <%= render_partial "form" %> > > <%= submit_tag "Save and new"%> > > <%= submit_tag "Save and return" %> > > <%= end_form_tag %> > > <%= link_to 'Back', :action => 'my_list' %> > > </td> > > <td width="40px"/> > > <td valign="top"> > > <%= render_partial "currentdaysummary" %> > > <br/> > > <%= render_partial "fiscalmonthsummary" %> > > </td> > > </tr> > > </table> > > The "validates presence of" doesn't seem to work at least to provide > user feedback. Records missing the activity are not saved but the user is > not given any visual feedback either. I have tried other things such as my > own validations, rescue code, etc… and the error is getting fired I just > can't seem to redirect the user back to his previous page (with all the data > still set on it) and provide them a useful message. This stuff works great > for my other forms (which don't have two submit options – Save and new vs. > Save, and two different views of the same form based on the record type, > working vs. non working). > > Any help? > > *Carol A. Rinauro* > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Agnieszka Figiel
2005-Sep-08 08:15 UTC
Re: Newbie rails user needs validation/error handling help
On 9/7/05, Bogdan Ionescu <bogdan.ionescu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well, I''m kind of a newbie myself, but normally if your save fails you > shouldn''t redirect your page to some other action, if you want to see the > form and the values. > So I would remove the else branch.If you need to redirect instead of render_action you might try to pass the object which failed to save in your session to the other action (clean the session afterwards) - not elegant, but that''s how I got it to work myself after quite a battle :-) you need to have the object (with its .errors) in the action to which you redirect. -- Agnieszka Figiel _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails