Ruby on rails 来自数据库和图像标签的Erb
只是偶然发现一个问题,到目前为止,无法解决它。下面是设置: 我从数据库中获取了一个ERB模板,并将其呈现为htmlRuby on rails 来自数据库和图像标签的Erb,ruby-on-rails,ruby,ruby-on-rails-3,asset-pipeline,erb,Ruby On Rails,Ruby,Ruby On Rails 3,Asset Pipeline,Erb,只是偶然发现一个问题,到目前为止,无法解决它。下面是设置: 我从数据库中获取了一个ERB模板,并将其呈现为html Class MyController < ApplicationController include AssetTagHelper ... def Show template=Page.find(...) # <%=image_tag('Test.png')%> @html=ERB.new(template).result(bin
Class MyController < ApplicationController
include AssetTagHelper
...
def Show
template=Page.find(...) # <%=image_tag('Test.png')%>
@html=ERB.new(template).result(binding)
end
...
Class MyController
现在的问题是image_标记'src'解析为'/images/Test.png',而通常它应该解析为'/assets/Test.png'。因此,我研究了rails的源代码,并找到了以下调用链:image\u path=>asset\u path=>compute\u asset\u path。并且compute_asset_path合法地声明它实际上应该解析为/images/Test.png
我错过了什么?我如何使图像标签工作并给我“assets/Test.png”
提前感谢所有回复 仅用于记录-在调试过程中,我们发现在sprockets-rails-2.0.1/lib/sprockets/rails/helper.rb中覆盖了通常的计算资源路径
通过将
@html=ERB.new(模板).result(绑定)
从控制器移动到视图,解决了该问题。希望这对其他人有所帮助)就像我展示的示例一样,我展示了如何从数据库中为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
从rails 3.2.13中可以完美地工作。早前没有尝试过。如果您知道它总是需要解决什么问题,为什么不直接使用它呢?因此,与其返回.erb并在呈现之前对其进行处理,为什么不将所需的html存储在数据库中呢?
include ActionView::Helpers::NumberHelper