簡體   English   中英

RAILS_ROOT_VIEW_PATH像RAILS_ROOT一樣的常量嗎?

[英]RAILS_ROOT_VIEW_PATH Constant, like RAILS_ROOT?

對於以下SAT風格的類比有答案嗎?

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

即,Rails中的路徑app/views是否有一個常量?

我問的原因是因為從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

注意:我執行顯式模板渲染的原因是ActionMailer :: Base文檔指出:“如果已將任何附件或部件添加到電子郵件中,則不會執行隱式模板渲染”,而我正在添加PDF附件。

另外,從調試器中,我發現view_paths.last給了我想要的東西,但它似乎是可變的。 我想要一個我知道每次都會起作用的常量。

另外,在調試器中,我可以鍵入p instance_variablesp.local_variables ,但是我看不到用於打印出可用常量的方法(在p puts methods.sort的輸出中)。 有一個嗎?

簡短的答案:不要那樣做。

這就像在PATH環境變量中要求單個路徑時,實際上其中可能有很多路徑。 您應該使用Rails的內置render功能來執行需要處理視圖文件的任何事情。 仔細考慮為什么需要知道該路徑。 您確定要這樣做,還是可以只使用render

您可能應該從控制器調用model方法,並傳遞通過controller_pathparams[:controller]獲得的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM