有什么方法可以将模型的属性定义为 html_safe 吗?
我有一个名为 Feature
的模型,其中包含一个名为 body_string
的变量,其中包含我想要渲染而不是转义的 HTML 标记。
每次在视图中引用 body_string
时,我都需要使用 <%=raw
或 .html_safe
。这看起来多余而且不那么干。
有什么方法可以将 body_string
变量一次性建立为 html_safe
吗?
我假设这会发生在 app/models/feature.rb
文件中,但我无法准确地弄清楚正确的语法是什么。我想到过这一点:
def body_string
return self.body_string.html_safe
end
但是 Rails 不喜欢它;它会引发堆栈级别太深
异常。
当然,我可以定义一个具有不同名称的变量/方法:
def safe_body_string
return self.body_string.html_safe
end
然后只需将视图中的所有引用从 body_string
更改为 safe_body_string
即可。但不知怎的,这看起来几乎和一开始就简单地使用 raw
或 .html_safe
一样不干燥。
关于如何最好地处理这个问题有什么见解吗?我觉得一定有一些非常优雅的东西我只是没有看到。
I have a model called Feature
with a variable called body_string
, which contains HTML markup I'd like to render, rather than escape.
Every time I reference body_string
in my views, I need to use <%=raw
or .html_safe
. This seems redundant and not-so-DRY.
Is there any way that I can establish once-and-for-all the body_string
variable as html_safe
?
I'm assuming this would happen in the app/models/feature.rb
file, but I can't figure out what the right syntax would be, exactly. I've thought of this:
def body_string
return self.body_string.html_safe
end
But Rails doesn't like it; it raises a stack level too deep
exception.
Naturally I could define a variable/method with a different name:
def safe_body_string
return self.body_string.html_safe
end
And then just change all references in the views from body_string
to safe_body_string
. But somehow this seems almost as un-DRY as simply using raw
or .html_safe
in the first place.
Any insights to how best to handle this? I feel like there must be something really elegant that I'm just not seeing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
read_attribute
即可避免递归调用body_string
:read_attribute
由write_attribute
用于在模型中设置属性。关于样式的注释:除非您确实需要,否则不要使用显式的
return
。方法中最后一条语句的结果隐式是从该方法返回的值。Just use
read_attribute
to avoid the recursive call tobody_string
:read_attribute
is complemented bywrite_attribute
for setting attributes from within your model.A note on style: Don't use explicit
return
s unless you actually need them. The result of the last statement in a method is implicitly the value returned from the method.虽然@meager的答案肯定会起作用,但我认为这个逻辑不属于模型。很简单,因为它向模型层添加了视图级别的关注点(HTML 安全性),而模型层应该只包含业务逻辑。相反,我建议使用 Presenter(请参阅 http://nithinbekal.com/posts/rails- Presenters/ 或者为此找到一个宝石 - 我个人喜欢展示柜)。您的演示者可以轻松重写
body_string
方法,并在视图中显示时提供.html_safe
指定。通过这种方式,您可以分离您的关注点,并可以继续从其他模型获取body_string
,而无需混合视图关注点。While @meager's answer will definitely work, I don't think this logic belongs in a model. Simply because it adds view-level concerns (HTML safeness) to the model layer, which should just include business logic. Instead, I would recommend using a Presenter for this (see http://nithinbekal.com/posts/rails-presenters/ or find a gem for this -- I personally love Display Case). Your presenter can easily override the
body_string
method and provide the.html_safe
designation when displaying in the view. This way you separate your concerns and can continue to getbody_string
from other models without mixing in the view concern.也许这个宝石对你有用。当内容完全可信时,我还想停止一直重复 html_safe。
http://rubygems.org/gems/html_safe_attribute
Maybe this gem is useful for you. I also wanted to stop repeating html_safe all the time when the content is completely trustable.
http://rubygems.org/gems/html_safe_attribute
或者你也可以使用这种方法,
Or you can also use this approach,