Hello, I am trying to define a before_save method in an object. I would like to call a function like sanitize or simple_format. These are view functions. Is there any way, short of writing my own functions, to sanitize a user''s text input BEFORE it is saved to the database rather than after? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mohit Sindhwani
2007-Aug-14 14:49 UTC
Re: Validation? Cool... How about before_save formatting
Mindtonic wrote:> Hello, > > I am trying to define a before_save method in an object. I would like > to call a function like sanitize or simple_format. These are view > functions. Is there any way, short of writing my own functions, to > sanitize a user''s text input BEFORE it is saved to the database rather > than after? > > Thanks! > >The ActiveRecord Callbacks for before_save and before_create might help: http://railsmanual.com/module/ActiveRecord::Callbacks Cheers, Mohit. 8/14/2007 | 10:49 PM. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sweet... What I am looking for are pre-existing methods that I can call from within before_save that will serve the same function as sanitize or simple_format. On Aug 14, 3:49 pm, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote:> Mindtonic wrote: > > Hello, > > > I am trying to define a before_save method in an object. I would like > > to call a function like sanitize or simple_format. These are view > > functions. Is there any way, short of writing my own functions, to > > sanitize a user''s text input BEFORE it is saved to the database rather > > than after? > > > Thanks! > > The ActiveRecord Callbacks for before_save and before_create might help:http://railsmanual.com/module/ActiveRecord::Callbacks > > Cheers, > Mohit. > 8/14/2007 | 10:49 PM.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
http://snipplr.com/view/2505/use-helpers-in-controllers-or-models/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks! That is exactly what I was looking for. Singleton no less, very interesting! I am of the philosophy that you should clean and format before you store in the database and this is an excellent way to do it. On Aug 14, 4:44 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> http://snipplr.com/view/2505/use-helpers-in-controllers-or-models/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
that snippet is actually a little more verbose than necessary but with the benefit of namespacing the methods into help.xxx() if you wanted you could simply require the ActionView helper module in your application.rb: include ActionView::Helpers::TextHelper and that''s it... just call "sanitize(''whatever'')" in your controllers directly On Aug 15, 6:19 am, Mindtonic <mindto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks! That is exactly what I was looking for. > > Singleton no less, very interesting! > > I am of the philosophy that you should clean and format before you > store in the database and this is an excellent way to do it. > > On Aug 14, 4:44 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >http://snipplr.com/view/2505/use-helpers-in-controllers-or-models/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Carl Johnson
2007-Aug-15 18:09 UTC
Re: Validation? Cool... How about before_save formatting
> I am of the philosophy that you should clean and format before you > store in the database and this is an excellent way to do it.IMHO it is a gigantic deficiency of Rails that there is not an out-of-the-box way to do this. h() is good but why filter output in 100 places when you can filter input once? -- 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 -~----------~----~----~----~------~----~------~--~---
> IMHO it is a gigantic deficiency of Rails that there is not an > out-of-the-box way to do this. h() is good but why filter output in 100 > places when you can filter input once?well, you can... exactly as described above or write your own methods. IMHO it''s not rails'' place to do this automatically - you might want to have those special characters in the db. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---