It's been a while since I did this.  There seemed to be a
few different ways to approach it, and I was never clear
on what "best practice" was, but what I ended up doing seemed
to give us the finest level of control.  Instead of letting things
happen implicitly with save() calls, I explicitly called the "valid?"
method on the object and directly went where I needed from
there based on the result.  The valid? call puts the error
data into the object.  Unless you want to mess with storing
stuff in the session, you can't redirect.  Instead you have
to render to the edit form so that the @posting object with
all the data is still available.  Something like

   if @posting.valid?
     @posting.save!
     render :action => :whatever
     return
   else
     # set up ancillary stuff for edit form, then:
     render :action => :edit
     return
   end

(it's been a little while, my ruby might be rusty :-)

Another approach if things are straightforward enough, you can just call
save! directly, which does the validation and saves or not accordingly.
Then handle the validation exception if you have one.  In our situation,
it was easier to get all the control choices we needed by going the first
way.

If that's not clear I can get details tomorrow.

        -glenn

liquid_rails wrote:
> In my project, a user creates a posting.  Before the posting is
> written to the database, I would like to validate that certain form
> fields have been completed according to validation rules in the
> posting.rb file, and allow the user to preview and edit their posting
> before writing it to the database.  If validation fails, I would like
> to redirect the user to the form where the errors are highlighted.
> Does anyone have any suggestions as to the best way to store, validate
> and pass fields and error messages for this temporary object?  The
> posting will be written to the database once the user has previewed
> and clicked the final submit button.
> 
> Thank you in advance.  Any suggestions would be greatly appreciated!
> > 

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to