Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it compatible with rails_api? #106

Closed
hcyildirim opened this issue Sep 7, 2018 · 3 comments
Closed

is it compatible with rails_api? #106

hcyildirim opened this issue Sep 7, 2018 · 3 comments

Comments

@hcyildirim
Copy link

Hi,

I want to export data to excel and send it to client who wants.

But there is problem in my setup, I am getting MissingTemplate error.

controller/posts_controller.rb

# GET /posts
  def index
    @posts = Post.all

    PostMailer.export(@posts, current_user).deliver

    render json: @posts
  end

views/posts/export.xlsx.axlsx

wb = xlsx_package.workbook
wb.add_worksheet(name: "Posts") do |sheet|
  posts.each do |post|
    sheet.add_row [post.title, post.content, post.creator.name]
  end
end

mailers/post_mailer.rb

class PostMailer < ApplicationMailer
  def export(posts, user)
    xlsx = render_to_string layout: false, handlers: [:axlsx], formats: [:xlsx], template: "posts/export", locals: {posts: posts}
    attachment = Base64.encode64(xlsx)
    attachments["Posts.xlsx"] = {mime_type: Mime[:xlsx], content: attachment, encoding: "base64"}

    mail(to: user.email, subject: "Posts") do |f|
      f.text do
        render text: nil
      end
    end
  end
end

What am I doing wrong?
Thanks.

@straydogstudio
Copy link
Collaborator

@ccoeder If you can get a sample repo with the problem it would help. I've not been able to track down this issue yet.

@hcyildirim
Copy link
Author

@straydogstudio Actually, the error I get is the same with #29

But thank you for response.

@straydogstudio
Copy link
Collaborator

Whatever is producing that error is an environment specific issue in combination with Rails 5 and the url called. Each time someone has reported that error, I've never been able to reproduce it with similar conditions. Hence my request for a sample repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants