# Overriding model's save method

**URL:** <https://www.ruby-forum.com/t/overriding-models-save-method/173802>\
**Category:** Rails\
**Created:** [August 15, 2009, 9:55am UTC](https://www.ruby-forum.com/t/overriding-models-save-method/173802 "2009-08-15T09:55:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommy\_Bergeron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@Tommy\_Bergeron](https://www.ruby-forum.com/u/Tommy_Bergeron)\
**Post date:** [August 15, 2009, 9:55am UTC](https://www.ruby-forum.com/t/overriding-models-save-method/173802/1 "2009-08-15T09:55:03Z")

</div>

Hi!

Okay, so let’s start I’m starting rails so here’s a noob question for  
you!

I did a little blog with scaffolding. Everything is working as  
expected but I’d like to modify it’s functionality.

I’ve been a PHP developer for ages and I’m currently using symfony for  
almost a year. It’s a PHP framework that works in some way like rails.

In my little blog I, sometimes, enter ruby codes with HTML tags (\<\>)  
but as there’s nothing to convert them to html entities they are not  
showing up on the page.

As you can see on [http://rails.tbergeron.com](http://rails.tbergeron.com) They are plain HTML tags  
in my html layout.

So here’s what I’d like to do:  
I’d like to override my model’s save method to put something like h()  
around my text so html could be converted to entities.

Could you help? That’d be awesome!

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Tommy\_Bergeron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@Tommy\_Bergeron](https://www.ruby-forum.com/u/Tommy_Bergeron)\
**Post date:** [August 15, 2009, 11:08am UTC](https://www.ruby-forum.com/t/overriding-models-save-method/173802/2 "2009-08-15T11:08:12Z")

</div>

On Aug 15, 1:32 am, Tommy Bergeron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my little blog I, sometimes, enter ruby codes with HTML tags (\<\>)  
> but as there’s nothing to convert them to html entities they are not  
> showing up on the page.
> 
> As you can see onhttp://rails.tbergeron.comThey are plain HTML tags  
> in my html layout.
> 
> So here’s what I’d like to do:  
> I’d like to override my model’s save method to put something like h()  
> around my text so html could be converted to entities.

This sounds like a possible job for before\_save. Personally though I’d  
store unsanitized text in the database and sanitize it when displaying  
(having escaped text in the database might make your editing bits  
rather more complicated).

Fred

---

<div class="post-metadata">

**Author:** ![Tommy\_Bergeron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@Tommy\_Bergeron](https://www.ruby-forum.com/u/Tommy_Bergeron)\
**Post date:** [August 16, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/overriding-models-save-method/173802/3 "2009-08-16T17:37:52Z")

</div>

On Aug 15, 4:52 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[…]

> Personally though I’d  
> store unsanitized text in the database and sanitize it when displaying  
> (having escaped text in the database might make your editing bits  
> rather more complicated).

Maybe. If you’re just using plain text, then just store it plain in  
the database and escape it on output. However, if you want to allow  
HTML tags for formatting, then the database should contain HTML  
fragments and _not_ be escaped on output.

Either way, though, h() on before\_save is probably a bad idea.

> Fred

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
