将 erb 从数据库渲染到视图中问题请帮忙!

发布于 2024-09-27 09:58:54 字数 309 浏览 0 评论 0原文

我正在数据库中保存一些 erb 并在视图中渲染它,如下所示:

erb = ERB.new(content)
render :text => erb.result

尝试渲染 erb 时遇到错误,该 erb 中的 image_tag 保存在数据库中。错误是:

undefined method `image_tag' for main:Object

有人帮忙解决这个问题吗?我也收到 stylesheet_link_tag 的错误?

非常感谢 里克

i am saving some erb in my database and rendering it in the view like this:

erb = ERB.new(content)
render :text => erb.result

I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :

undefined method `image_tag' for main:Object

Anyone help on this ? i also get the error with the stylesheet_link_tag ?

Thank alot
rick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动次打次papapa 2024-10-04 09:58:54

我认为您需要将可选的绑定参数传递给 ERB::render 方法。这有效地提供了 ERB 模板范围内的局部变量。换句话说,绑定需要向模板提供 image_tag 变量。

我不知道您的情况下的“内容”是什么,但以下内容将从“父”视图传递绑定,假设 @obj.image_tag 从该视图可见:

<%= ERB.new("image tag - \<\%= @obj.image_tag \%\>").result(binding) %>

I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.

I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that @obj.image_tag is visible from that view:

<%= ERB.new("image tag - \<\%= @obj.image_tag \%\>").result(binding) %>
旧故 2024-10-04 09:58:54

这是因为你的控制器中没有助手。您需要包括您使用的所有助手。

It's because you haven't helper in your controller. You need include all Helper do you use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文