偶然发现问题,到目前为止,无法解决问题。所以这是设置:
我从数据库中获取了一个ERB模板,并将其渲染为html
Class MyController < ApplicationController
include AssetTagHelper
...
def Show
template=Page.find(...) # <%=image_tag('Test.png')%>
@html=ERB.new(template).result(binding)
end
...
现在问题是image_tag的'src'解析为'/images/Test.png',正常情况下应解析为'/assets/Test.png'。所以我查看了AssetTagHelper的导轨来源,这导致我AssetUrlHelper以及以下调用链:image_path =&gt; asset_path =&gt; compute_asset_path。而compute_asset_path合法地声明它应该实际解析为/images/Test.png ...
我在这里缺少什么?如何使图像标签工作并给我'assets / Test.png'?
提前感谢所有回复!
答案 0 :(得分:2)
仅供记录 - 在调试时发现通常在sprockets-rails-2.0.1 / lib / sprockets / rails / helper.rb中覆盖compute_asset_path
通过将@html=ERB.new(template).result(binding)
从控制器移动到视图来解决问题。希望这有助于某人))
答案 1 :(得分:1)
与示例一样,我展示了如何从Mailer类的数据库创建ERB。对于其他类同样。
已完成邮件程序以从数据库创建电子邮件模板:
class UserMailer < ActionMailer::Base
# included helper
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::TextHelper
# another helpers...
# included helper
def mailer(from, to, subject, path, name)
mail( from: from, to: to, subject: subject, template_path: path, template_name: name )
end
def get_template(template_name)
@erb = EmailTemplate.where(name: template_name, mailer_name: UserMailer.to_s.underscore).first rescue ''
@template_content_html = ERB.new(@erb.template_html).result(binding).html_safe rescue ''
@template_content_text = ERB.new(@erb.template_text).result(binding).html_safe rescue ''
end
def test(user_id)
from = 'from@mail.com'
recipients = 'to@mail.com'
subject = "test"
template_path = "user_mailer"
get_template(__method__) #def
template_name = "general"
mailer(from, recipients, subject, template_path, template_name)
end
end
对于Mailer中的include helper,你可以使用这样的结构:
include ActionView::Helpers::NumberHelper
从rails 3.2.13完美运行。早些时候没试过。