padrino / padrino-framework

Padrino is a full-stack ruby framework built upon Sinatra.
http://www.padrinorb.com
MIT License
3.37k stars 509 forks source link

Possible bug with partials in email templates #2231

Open CyberFerret opened 4 years ago

CyberFerret commented 4 years ago

In Padrino 0.14.4 running on Ruby 2.5, I am using partial to add HAML snippets into emails I am sending out using Mailer.

Folder structure is as follows:

/app
  |
   - views
      |
       - mailers
          |
           - worker_mailer
              |
               - layouts
                   |
                    - email_layout.haml
              |
               - my_email.haml
               - _snippet1.haml
               - _snippet2.haml

So I set up within worker_mailer.rb:

email :my_email do |recipient, sender, company|
  from          "me@mycompany.com"
  to            recipient
  reply_to      sender
  subject       'This is my subject line!'
  content_type  'text/html'
  via           :smtp
  render        :my_email, :layout => 'email_layout', :locals => {:company => ...etc... }
end

(Side note: Wish we could use the locals declaration within the email block, but it looks like if we are rendering using templates, we have to move the locals to a parameter within the render line?!)

Then in the my_email.haml template, I have partials within the document like so:

%p Blah...
%div
  =partial 'snippet1', :engine => :haml, :locals => {...}
  %span Blah...
  =partial 'snippet2', :engine => :haml, :locals => {...}

However when trying to send the email, I am getting the error:

Errno::ENOENT: No such file or directory @ rb_sysopen - /<redacted>/myapp/app/views/mailers/worker_mailer/worker_mailer/_snippet1.haml

(Note the duplicated 'worker_mailer' folder in the error message).

If I then try and create a subfolder called 'worker_mailer' under the existing 'worker_mailer' folder and put _snippet1.haml in there, then the first line will pass OK, but it throws an error on the next partial with:

Errno::ENOENT: No such file or directory @ rb_sysopen - /<redacted>/myapp/app/views/mailers/worker_mailer/worker_mailer/worker_mailer/_snippet2.haml

(Note that the 'worker_mailer' folder appears to be recursively adding to itself for each partial in the template!)

I've tried hard coding the path to the partial template, but with no success. The rendering routine seems to want to keep adding another 'worker_mailer' to the parent folder for each subsequent partial. i.e.:

  =partial '/worker_mailer/snippet1', :engine => :haml, :locals => {...}

still doesn't work.

Is this a bug in rendering.rb or am I doing something wrong?

nesquena commented 4 years ago

Sorry to hear about this issue. Unfortunately I don't think we had tested partials in email templates much, or at least I don't recall testing that very much. I could see this being a fairly easy fix. Would be happy to merge in a fix if someone sees this and decides to take a crack at it and open a PR.