laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Feb-13 18:20 UTC
Doing error checking before saving my object to db?
Hi, I have two models -- order and line_item (with attributes item_number and description) where an order has many line_items. In my order controller, I have this action after a user has submitted his order: def summary @ec_order = EcOrder.new(params[:ec_order]) # do some error checking session[:ec_order] = @ec_order end Prior to saving the submitted order to the session, I would like to do some error checking to make sure none of the line_item.item_number properties are empty or non-numeric. I already have this in my model: class LineItem < ActiveRecord::Base belongs_to :order validates_presence_of :item_number validates_numericality_of :item_number, :integer_only => true end How can I activate this error checking for the line_item object or is it only activated when I try and save the whole thing to the database? Thanks, - Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
you can do: if @ec.order.valid? session[:ec_order] = @ec_order end -- 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-/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 -~----------~----~----~----~------~----~------~--~---
laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Feb-13 19:45 UTC
Re: Doing error checking before saving my object to db?
Gold. Thanks, Phil. A follow up, if it''s not valid, how can I get a specific error message? For example, if a user didn''t enter an item number, I''d figure an error message would get generated somewhere because of the "validates_presence_of :item_number" in the line_item model. - Dave On Feb 13, 1:08 pm, Phil Tayo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> you can do: > > if @ec.order.valid? > session[:ec_order] = @ec_order > end > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Objects should not be stored in session. Only ids are stored. Http://www.rubyplus.org Free Ruby & Rails screencasts On Feb 13, 2008, at 11:45 AM, "laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org" <laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org > wrote:> > Gold. Thanks, Phil. > > A follow up, if it''s not valid, how can I get a specific error > message? For example, if a user didn''t enter an item number, I''d > figure an error message would get generated somewhere because of the > "validates_presence_of :item_number" in the line_item model. > > - Dave > > On Feb 13, 1:08 pm, Phil Tayo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> you can do: >> >> if @ec.order.valid? >> session[:ec_order] = @ec_order >> end >> -- >> Posted viahttp://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-/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 -~----------~----~----~----~------~----~------~--~---
um I think you need to look at "error_messages_for" http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005 have a google around and you''ll see how it works. also: i dunno what BCP is trying to say but he''s probably right, I wouldn''t know about how to do things properly. I''m only good at getting things to work at all :D -- 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-/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 -~----------~----~----~----~------~----~------~--~---
laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Feb-13 23:58 UTC
Re: Doing error checking before saving my object to db?
Phil, I''ve read over the link. Thanks for sending it. My question is I don''t know what to put as the params value for error_messages_for. I tried "error_messages_for(@ec_order)" but that produced an error. Here are my methods. I start on "new" and submit to "summary", which should kick it back to "new" if there were errors ... def new @user = User.find(session[:user_id]) if (session[:ec_order] != nil) @ec_order = session[:ec_order] else @ec_order = EcOrder.new 1.times { @ec_order.ec_line_items.build } end end def summary @ec_order = EcOrder.new(params[:ec_order]) if (!@ec_order.valid?) flash[:notice] = "Order is invalid." redirect_to :action => ''new'' else session[:ec_order] = @ec_order end end BCP, I''m not storing ids because I haven''t stored my object to the database yet. I want to give the user a screen to confirm before the data gets stored. So that''s why I''m storing to a session. But if there''s a better way, please let me know. Thanks to all, - Dave BCP, But I haven''t stored anything to the database yet, so I don''t have an id. On Feb 13, 2:50 pm, Phil Tayo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> um I think you need to look at "error_messages_for"http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHel... > > have a google around and you''ll see how it works. > > also: i dunno what BCP is trying to say but he''s probably right, I > wouldn''t know about how to do things properly. I''m only good at getting > things to work at all :D > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
well the way i''m using it in my current project (my first in RoR :D) is that in a view somewhere i have a form like this: <%form_for :user do |form|%> some code <%=error_messages_for "user" %> some more code <% end %> so when you submit, if there are any errors on the submitted object, the controller returns you to this view, populates the error_messages_for and displays them to screen. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
ok i did some reading and it turns out that the session variable should only be used to store the user_id for a logged in user -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Feb-14 12:03 UTC
Re: Doing error checking before saving my object to db?
validates_associated also works. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---