ma11hew28
ma11hew28

Reputation: 126357

RAILS_ROOT_VIEW_PATH Constant, like RAILS_ROOT?

Is there an answer to the following SAT-style analogy?

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

I.e., 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 "[email protected]"
    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?

Upvotes: 0

Views: 485

Answers (2)

count0
count0

Reputation: 2621

You should probably call the model method from a controller and pass the path obtained through controller_path or params[:controller].

Upvotes: 0

Brian Donovan
Brian Donovan

Reputation: 8400

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?

Upvotes: 2

Related Questions