简体   繁体   English

RAILS_ROOT_VIEW_PATH像RAILS_ROOT一样的常量吗?

[英]RAILS_ROOT_VIEW_PATH Constant, like RAILS_ROOT?

Is there an answer to the following SAT-style analogy? 对于以下SAT风格的类比有答案吗?

. : RAILS_ROOT :: ./app/views : ???

Ie, is there a constant in Rails for the path app/views ? 即,Rails中的路径app/views是否有一个常量?

The reason I'm asking is because from app/models/notifier.rb , I'm trying to render the body of an email with a file: 我问的原因是因为从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

Note: the reason I'm doing explicit template rendering is that the ActionMailer::Base documentation states, "Implicit template rendering is not performed if any attachments or parts have been added to the email," and I'm adding a PDF attachment. 注意:我执行显式模板渲染的原因是ActionMailer :: Base文档指出:“如果已将任何附件或部件添加到电子邮件中,则不会执行隐式模板渲染”,而我正在添加PDF附件。

Also, from the debugger, I find that view_paths.last gives me what I want, but it seems variable. 另外,从调试器中,我发现view_paths.last给了我想要的东西,但它似乎是可变的。 I want something constant that I know will work every time. 我想要一个我知道每次都会起作用的常量。

Also, from the debugger, I can type p instance_variables & p.local_variables , but I don't see a method (in the output of p puts methods.sort ) for printing out the available constants. 另外,在调试器中,我可以键入p instance_variablesp.local_variables ,但是我看不到用于打印出可用常量的方法(在p puts methods.sort的输出中)。 Is there one? 有一个吗?

Short answer: don't do that. 简短的答案:不要那样做。

That's like asking for the single path in the PATH environment variable when in reality there are probably many paths in it. 这就像在PATH环境变量中要求单个路径时,实际上其中可能有很多路径。 You should be using Rails' built-in render function to do anything that needs to deal with view files. 您应该使用Rails的内置render功能来执行需要处理视图文件的任何事情。 Carefully consider why you need to know that path. 仔细考虑为什么需要知道该路径。 Are you sure you do, or can you just use render ? 您确定要这样做,还是可以只使用render

您可能应该从控制器调用model方法,并传递通过controller_pathparams[:controller]获得的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM