Ruby on rails RAILS\u ROOT\u VIEW\u PATH常量,比如RAILS\u ROOT?
以下SAT风格的类比有答案吗Ruby on rails RAILS\u ROOT\u VIEW\u PATH常量,比如RAILS\u ROOT?,ruby-on-rails,ruby,view,constants,Ruby On Rails,Ruby,View,Constants,以下SAT风格的类比有答案吗 . : RAILS_ROOT :: ./app/views : ??? 也就是说,对于路径app/views,Rails中是否有一个常量 我问这个问题的原因是因为在app/models/notifier.rb中,我试图用一个文件呈现电子邮件的正文: def notify_fact_act(user, domain, filename) subject "Email with multipart/alternative & attachment"
. : RAILS_ROOT :: ./app/views : ???
也就是说,对于路径app/views
,Rails中是否有一个常量
我问这个问题的原因是因为在app/models/notifier.rb中,我试图用一个文件呈现电子邮件的正文:
def notify_fact_act(user, domain, filename)
subject "Email with multipart/alternative & attachment"
recipients user.email
from "rails@example.com"
content_type "multipart/mixed"
file = File.join(view_paths.last, mailer_name, @template+'.text.')
body = {:user => user, :domain => domain}
part :content_type => "multipart/alternative" do |p|
p.part :content_type => "text/plain",
:body => render(:file => file + "plain.erb", :body => body)
p.part :content_type => "text/html",
:body => render(:file => file + "html.erb", :body => body)
end
attachment :content_type => "application/pdf",
:body => File.read(filename),
:filename => File.basename(filename)
end
注意:我执行显式模板呈现的原因是,状态为“如果电子邮件中添加了任何附件或部分,则不会执行隐式模板呈现”,并且我正在添加PDF附件
另外,在调试器中,我发现view\u path.last
提供了我想要的,但它似乎是可变的。我想要一些我知道每次都能用的恒久不变的东西
此外,在调试器中,我可以键入
p instance_variables
&p.local_variables
,但我没有看到打印可用常量的方法(在p puts methods.sort
的输出中)。有吗?简短的回答:不要那样做
这就像在
path
环境变量中请求一条路径,而实际上其中可能有许多路径。您应该使用Rails的内置函数来处理视图文件。仔细考虑为什么你需要知道这条路。您确定要这样做,还是可以只使用render
?您可能应该从控制器调用model方法,并传递通过controller\u path
或params[:controller]
获得的路径,我使用的是render
,但来自模型。看看我对这个问题的更新。我几乎可以肯定有更好的方法来解决这个问题,但我不确定这是什么。