How do I format text before saving it?


#1

I tried to use a before_save and use simple_format, it returns and
undefined method error. Can I even use these methods within a model? I
tried to use auto_link and it failed too. What am I doing wrong?

Furthermore, would it be recommended I format text before saving it to
the database? Advantages or disadvantages?


#2

Eleo wrote:

I tried to use a before_save and use simple_format, it returns and
undefined method error. Can I even use these methods within a model? I
tried to use auto_link and it failed too. What am I doing wrong?

Furthermore, would it be recommended I format text before saving it to
the database? Advantages or disadvantages?

I guess you could but you need to point to the helper in your model:
helper :text_helper

but maybe it’s better to format in the view where you want to display
the given text, if for instance you would like to be able to edit it in
a simple text field it might be confusing for the user to see tags
there.


Agnieszka


#3

Assuming that your text is only going to be interpreted by your
application’s view, where you can control the interpretation, it seems
more efficient to save the unformatted text to the database.

If another application is accessing the database or using the text, and
you don’t have control over how that app formats the text, then it might
make sense to pre-format it, but otherwise, why not just call
“simple_format” in your view before you display the text?

Jeff

Agnieszka F. wrote:

Eleo wrote:

I tried to use a before_save and use simple_format, it returns and
undefined method error. Can I even use these methods within a model? I
tried to use auto_link and it failed too. What am I doing wrong?

Furthermore, would it be recommended I format text before saving it to
the database? Advantages or disadvantages?

I guess you could but you need to point to the helper in your model:
helper :text_helper

but maybe it’s better to format in the view where you want to display
the given text, if for instance you would like to be able to edit it in
a simple text field it might be confusing for the user to see tags
there.


Agnieszka