Christoffer
Christoffer

Reputation: 2411

Use rails helper methods in database text

Using Rails 3.1.1

I am creating a travel guide that typically consist of "articles". In these articles I write about each place. Each article is about 500 words long and is saved as the attribute article.content in the database.

Now, I would prefer to be able to use Rails helper methods (i.e. from application_helper) and "link_to" within these articles. I can't use <%= %> simply because Rails will just interpret this as text in the article.

The main reason for me wanting to do so is to use smart internal linking (routes) and helper methods.

To clarify further:

Article is a model which has an content attribute.

a = Article.first
z = Article.last
a.content = "This is a long article where I want to place a smart link to z by using <%= link_to 'z article', article_path(z) %> and use my helper method largify so that I can <%= largify('this text') %> but I can't. What should I do?"
a.save

Is there a smart way of solving this?

Upvotes: 0

Views: 116

Answers (1)

jdoe
jdoe

Reputation: 15771

<%= render inline: a.content, type: :erb %>

But beware of filling your database from untrusted sources -- someone can use it to place malicious code between <%= %>.

Upvotes: 1

Related Questions