caxlsx_rails icon indicating copy to clipboard operation
caxlsx_rails copied to clipboard

Rails 5 + Mailer: MissingTemplate Error

Open jonbcampos opened this issue 8 years ago • 20 comments

I know this is showing up in other issues but, maybe I'm missing something, but in my tests I'm still getting an error:

Minitest::UnexpectedError: ActionView::MissingTemplate: Missing template layouts/mailer with {:locale=>[:en], :formats=>[:xlsx], :variants=>[], :handlers=>[:axlsx]}. Searched in:
  * "/Users/myuser/Documents/code/myapp/app/views"
  * "/Users/myuser/.rvm/gems/ruby-2.3.1/gems/pghero-1.6.2/app/views"
  * "/Users/myuser/.rvm/gems/ruby-2.3.1/gems/resque-web-0.0.9/app/views"
  * "/Users/myuser/.rvm/gems/ruby-2.3.1/gems/twitter-bootstrap-rails-3.2.2/app/views"
  * "/Users/myuser/.rvm/gems/ruby-2.3.1/gems/devise_invitable-1.7.0/app/views"
  * "/Users/myuser/.rvm/gems/ruby-2.3.1/gems/devise-4.2.0/app/views"

My code for the mailer:

def batch_transfer_complete(batch_transfer_id)
    @batch_transfer                          = BatchTransfer.find(batch_transfer_id)
    admins                                   = Admin.pluck(:email)
    xlsx                                     = render_to_string 
                                                                handlers: [:axlsx],
                                                                formats:  [:xlsx],
                                                                template: 'batch_transfers/show',
                                                                locals:   { batch_transfer: @batch_transfer }
    attachments[@batch_transfer.to_filename] = { mime_type: Mime::XLSX, content: xlsx, encoding: 'base64' }
    mail to:      admins,
         subject: t('admin_mailer.batch_transfer_complete.subject', model: BatchTransfer.model_name.human.titleize)
  end

Any ideas of what I may be missing?

jonbcampos avatar Jan 11 '17 15:01 jonbcampos

And I've tests calling the endpoint directly and I get the xlsx download no problem

jonbcampos avatar Jan 11 '17 15:01 jonbcampos

Have you tried: self.instance_variable_set(:@_lookup_context, nil) after you set your attachment?

Is this running through Resque?

straydogstudio avatar Jan 11 '17 16:01 straydogstudio

I didn't but I did now to answer you fully and yes I get the same error with the addition.

I am running with Resque but this is showing up in my MiniTest, I haven't even gotten to running it yet due to the error.

On Wed, Jan 11, 2017 at 10:28 AM, Noel Peden [email protected] wrote:

Have you tried: self.instance_variable_set(:@_lookup_context, nil) after you set your attachment?

Is this running through Resque?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/straydogstudio/axlsx_rails/issues/65#issuecomment-271916624, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIgzX0hd0BrV0vkhA7EruDtGmUdB6iFks5rRQMSgaJpZM4LgvO9 .

-- Jonathan Campos

jonbcampos avatar Jan 11 '17 16:01 jonbcampos

I just tested in the email preview and I get the same error (just an fyi)

jonbcampos avatar Jan 11 '17 19:01 jonbcampos

What are your versions of rails, axlsx, axlsx_rails, and rubyzip?

straydogstudio avatar Jan 11 '17 20:01 straydogstudio

axlsx_rails (0.5.0) rubyzip (1.1.7) axlsx (2.1.0.pre) rails (5.0.1)

On Wed, Jan 11, 2017 at 2:22 PM, Noel Peden [email protected] wrote:

What are your versions of rails, axlsx, axlsx_rails, and rubyzip?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/straydogstudio/axlsx_rails/issues/65#issuecomment-271983074, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIgzcYd_VbBsPILdlNW0URK07GZLPCoks5rRToSgaJpZM4LgvO9 .

-- Jonathan Campos

jonbcampos avatar Jan 11 '17 20:01 jonbcampos

Would you try 0.4.0? It will throw some deprecations, but should still work with Rails 5.

straydogstudio avatar Jan 11 '17 23:01 straydogstudio

same error with downgrade

jonbcampos avatar Jan 12 '17 03:01 jonbcampos

@straydogstudio any other ideas?

jonbcampos avatar Jan 13 '17 17:01 jonbcampos

@jonbcampos Unfortunately no. Not without me trying to get the same error and chasing it down. If you have time to set up a repo that reproduces this code, I can do it. Otherwise it may be a while.

I've tried to find this error and so far not had any luck. It's an odd one that started with Rails 4. They made changes with the layout context. I will try to spend some time in the next week digging on the error.

straydogstudio avatar Jan 13 '17 23:01 straydogstudio

I'll see what I can do to create a paired down setup in the meantime.

On Jan 13, 2017 5:44 PM, "Noel Peden" [email protected] wrote:

@jonbcampos https://github.com/jonbcampos Unfortunately no. Not without me trying to get the same error and chasing it down. If you have time to set up a repo that reproduces this code, I can do it. Otherwise it may be a while.

I've tried to find this error and so far not had any luck. It's an odd one that started with Rails 4. They made changes with the layout context. I will try to spend some time in the next week digging on the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/straydogstudio/axlsx_rails/issues/65#issuecomment-272578068, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIgzXdD4XCefr23PR_ojPatnnEsc_T4ks5rSAxNgaJpZM4LgvO9 .

jonbcampos avatar Jan 13 '17 23:01 jonbcampos

@jonbcampos Did you get anywhere with this? Have you tried passing layout: false to render to string?

straydogstudio avatar Feb 13 '17 22:02 straydogstudio

I believe I had tried it. In the end we ended up (for other reasons) just switching to CSV so I dropped the effort. I know that isn't great for you but that is where it is

jonbcampos avatar Feb 13 '17 22:02 jonbcampos

Old issue but here's a fix:

mail(to: '[email protected]', subject: "Export") do |f|
  f.text do
    render text: nil # In order to don't use a template
  end
end

couraudt avatar May 30 '17 10:05 couraudt

@sweetdub Thanks for the note. What versions of rails/axlsx/axlsx_rails are you using?

straydogstudio avatar May 30 '17 12:05 straydogstudio

You're welcome @straydogstudio! I'm using those versions:

rails (5.0.2)
axlsx (2.1.0.pre)
axlsx_rails (0.5.1)

couraudt avatar May 30 '17 12:05 couraudt

+1

elisoncampos avatar Jul 20 '17 15:07 elisoncampos

With Rails 5.1 also seeing this Missing template error. As a quick fix I renamed my view from index.xls.axlsx to index.axlsx.

kakoni avatar Nov 08 '17 12:11 kakoni

Thank you so much @couraud this error has had me pulling my hair out all day! Workaround or not, this has my attachment sending with actual data. Same versions btw.

abadfish avatar Mar 29 '18 20:03 abadfish

Same issue.

hcyildirim avatar Sep 07 '18 13:09 hcyildirim