Is there an answer to the following SAT-style analogy?
. : RAILS_ROOT :: ./app/views : ???
Ie, is there a constant in Rails for the path 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:
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.
Also, from the debugger, I find that view_paths.last
gives me what I want, but it seems variable. 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. 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. You should be using Rails' built-in render
function to do anything that needs to deal with view files. Carefully consider why you need to know that path. Are you sure you do, or can you just use render
?
您可能应该从控制器调用model方法,并传递通过controller_path
或params[:controller]
获得的路径。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.