caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
742 stars 84 forks source link

is it compatible with rails_api? #106

Closed hcyildirim closed 6 years ago

hcyildirim commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@straydogstudio Actually, the error I get is the same with https://github.com/straydogstudio/axlsx_rails/issues/29

But thank you for response.

straydogstudio commented 6 years ago

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.