【问题标题】:Use rails helper methods in database text在数据库文本中使用 rails 辅助方法
【发布时间】:2012-04-29 17:12:37
【问题描述】:

使用 Rails 3.1.1

我正在创建一个通常由“文章”组成的旅行指南。在这些文章中,我写了每个地方。每篇文章大约 500 字长,在数据库中保存为 article.content 属性。

现在,我希望能够在这些文章中使用 Rails 辅助方法(即来自 application_helper)和“link_to”。我不能使用 只是因为 Rails 只会将其解释为文章中的文本。

我想要这样做的主要原因是使用智能内部链接(路由)和辅助方法。

进一步澄清:

Article 是一个具有 content 属性的模型。

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

有解决这个问题的聪明方法吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:
    <%= render inline: a.content, type: :erb %>
    

    但请注意不要从不受信任的来源填充您的数据库 - 有人可以使用它在 &lt;%= %&gt; 之间放置恶意代码。

    【讨论】:

    • 太好了,效果很好!数据库文本完全由我创建,所以应该没问题。
    • 您是否考虑过像Redcarpet(Markdown + 您自己的扩展名)这样更安全的替代方案?
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2012-12-30
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多