Forum: Ruby on Rails Preventing data being written to the database on error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0ffe9ac1df7f1545ffef601f0bbfdd8c?d=identicon&s=25 Gunt (Guest)
on 2006-04-06 05:12
Hello,

How do I stop data being written to the database when an error occurs?

I have this in my controller:

if @item.valid?
    #stuff here
else
    render :action => 'show', :id => @item
end

When it enters the else section, it renders the 'show' action, but it
adds the data to the database. How do I prevent this from happening?

Thanks
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-04-06 05:44
(Received via mailing list)
On Thu, Apr 06, 2006 at 05:12:17AM +0200, Gunt wrote:
> end
>
> When it enters the else section, it renders the 'show' action, but it
> adds the data to the database. How do I prevent this from happening?

Well, if your @item isn't valid, it shouldn't be successfully saved.
There's something funny going on otherwise.

Your example doesn't show where your save method is getting saved, or if
there's some data manpulation -- or even what your validation consists
of.
Knowing that stuff would help quite a bit.

- Matt
This topic is locked and can not be replied to.