Hey- I''m trying to hack together an "undo" function using the scaffolding code. With my limited knowledge of Ruby, Rails and object oriented programming, I''m using a global variable to temporarily store a copy of the contents of a record before it is destroyed and create a new record if the user decides they really did not want to destroy the record using what I have stored temporarily. Here is the code so far: ------------------------------------ categories_controller.rb ------------------------------------ def destroy $temp = Category.find(@params[''id'']).category if Category.find(@params[''id'']).destroy flash[''undo''] = ''Undo Delete '' redirect_to :action => ''list'' end end def undo @category = Category.new @category.category = $temp if @category.save flash[''notice''] = ''Undo was successfull.'' redirect_to :action => ''list'' else flash[''notice''] = ''Undo failed.'' redirect_to :action => ''list'' end end ------------------------------------ list.rhtml ------------------------------------ <% if @flash["undo"] %> <span class="undo"> <%= link_to ''Undo Destroy'', :action => ''undo'' %> </span> <% end %> ------------------------------------ application.rb ------------------------------------ class ApplicationController < ActionController::Base $temp = " " end .... I''m sure there is a more elegant way to do this. But the problem I''m running into is the $temp variable keeps getting reinitialized. The same thing happened when I tried to use a class variable. Is it possible to do what I''m trying to do with a class or global variable? What is the slick way to do this. I looked into cloning and rolling back the db transaction, but did not have enough info to go on. Thanks, kd
You could save it as a session variable, read more about that here: http://ap.rubyonrails.com/classes/ActionController/Base.html
If you need undo functionality why don''t you just flag records as deleted in the db. You can even override the destroy method to set this flag instead of really destroying the data. If you want to time out records after a while use a datetime as deleted flag and run a cron job which removes records which have a deleted flag with a datetime older than say 7 days. Welcome to OOP, never use global variables. On Wed, 30 Mar 2005 01:36:42 +0200, Jan Andersson <jan.andersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You could save it as a session variable, read more about that here: > http://ap.rubyonrails.com/classes/ActionController/Base.html > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
I got it to work by replacing $temp with cookies["category"]. Check ActionController::Cookies for more info. kd On Tue, 29 Mar 2005 17:13:16 -0500, Todd Grimason <todd-cwT7Wi5Y1r1eoWH0uzbU5w@public.gmane.org> wrote:> * Keith Donaldson [2005-03-29 17:07]: > > > > > .... I''m sure there is a more elegant way to do this. But the problem > > I''m running into is the $temp variable keeps getting reinitialized. > > The same thing happened when I tried to use a class variable. Is it > > possible to do what I''m trying to do with a class or global variable? > > What is the slick way to do this. I looked into cloning and rolling > > back the db transaction, but did not have enough info to go on. > > > > I can''t provide the ideal way but you''re going to get flakey at best > behavior this way since you have no guarantee you''ll get the same > FastCGI instance of your rails app on the next request. And if you''re > trying this with CGI everything is wiped between requests. > > I''d assume stashing it in the DB or some other persistent store is the > way to go... > > -- > > ______________________________ > toddgrimason*todd-AT-slack.net > >
I''m not sure what the best practice in ruby is about global variables, but I''d suggest avoiding them for this case. Perhaps for configuration variables it''d be acceptable. - Philip On Tue, 29 Mar 2005 19:32:16 -0500, Keith Donaldson <keith.donaldson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I got it to work by replacing $temp with cookies["category"]. Check > ActionController::Cookies for more info. > > kd > > On Tue, 29 Mar 2005 17:13:16 -0500, Todd Grimason <todd-cwT7Wi5Y1r1eoWH0uzbU5w@public.gmane.org> wrote: > > * Keith Donaldson [2005-03-29 17:07]: > > > > > > > > .... I''m sure there is a more elegant way to do this. But the problem > > > I''m running into is the $temp variable keeps getting reinitialized. > > > The same thing happened when I tried to use a class variable. Is it > > > possible to do what I''m trying to do with a class or global variable? > > > What is the slick way to do this. I looked into cloning and rolling > > > back the db transaction, but did not have enough info to go on. > > > > > > > I can''t provide the ideal way but you''re going to get flakey at best > > behavior this way since you have no guarantee you''ll get the same > > FastCGI instance of your rails app on the next request. And if you''re > > trying this with CGI everything is wiped between requests. > > > > I''d assume stashing it in the DB or some other persistent store is the > > way to go... > > > > -- > > > > ______________________________ > > toddgrimason*todd-AT-slack.net > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Don''t use a cookie.... cookies are serialized to the client and sent back to the server on each and every request the user does. Also that might even be a security hole since if a user deletes say a credit card he can read the cc number from his cookies. Really, just use a deleted column. On Tue, 29 Mar 2005 19:32:16 -0500, Keith Donaldson <keith.donaldson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I got it to work by replacing $temp with cookies["category"]. Check > ActionController::Cookies for more info. > > kd > > On Tue, 29 Mar 2005 17:13:16 -0500, Todd Grimason <todd-cwT7Wi5Y1r1eoWH0uzbU5w@public.gmane.org> wrote: > > * Keith Donaldson [2005-03-29 17:07]: > > > > > > > > .... I''m sure there is a more elegant way to do this. But the problem > > > I''m running into is the $temp variable keeps getting reinitialized. > > > The same thing happened when I tried to use a class variable. Is it > > > possible to do what I''m trying to do with a class or global variable? > > > What is the slick way to do this. I looked into cloning and rolling > > > back the db transaction, but did not have enough info to go on. > > > > > > > I can''t provide the ideal way but you''re going to get flakey at best > > behavior this way since you have no guarantee you''ll get the same > > FastCGI instance of your rails app on the next request. And if you''re > > trying this with CGI everything is wiped between requests. > > > > I''d assume stashing it in the DB or some other persistent store is the > > way to go... > > > > -- > > > > ______________________________ > > toddgrimason*todd-AT-slack.net > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog