1

I need to create a .docx file from a HTML template, so I used htmltoword gem.

Usage:

I added the gem (Gemfile):

gem 'htmltoword', '~> 0.5.1' #last version of the gem

I put a route (route.rb):

get 'preview' => 'foo#preview'

And in my bar.html.erb I have a link which target's that url:

<%= link_to '.docx', preview_path %>

Template (preview.docx.erb):

<h1>foobar</h1>

And in the controller (foos_controller.rb):

class FoosController < ApplicationController
  respond_to :docx

  #other code

  def preview
    respond_to do |format|
      format.docx do
        render docx: 'foobar', filename: 'preview.docx'
      end
    end
  end
end

However, I'm getting an error:

ActionController::UnknownFormat

How to fix this error?

My config:
RoR v4.2.4
Ruby v2.2.3p173


Also, there is an open github issue for this/similar topic.


Update: as @kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem, however, I get the same error.

Vucko
  • 20,555
  • 10
  • 56
  • 107

4 Answers4

0

Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use formatter gem..

For further details you can look to the link given below.

Why is respond_with being removed from rails 4.2 into it's own gem?

Community
  • 1
  • 1
kajal ojha
  • 1,248
  • 8
  • 20
0

Have you tried caracal-rails? You can find it here

0

I had to build this same functionality in an app earlier this year and also used the htmltoword gem.

# At the top of the controller: 
respond_to :html, :js, :docx

def download
  format.docx {
    filename: "#{dynamically_generated_filename}",
    word_template: 'name_of_my_word_template.docx')
  }
end

I then have two "view" files that come into play. The first, is my method view file download.docx.haml. This file contains the following code:

%html
  %head
    %title Title
  %body
    %h1 A Cool Heading
    %h2 A Cooler Heading
    = render partial: 'name_of_my_word_template', locals: { local_var: @local_var }

From there, I have another file name_of_my_word_template.docx.haml that contains the meat of my Word file.

%h4 Header
%h5 Subheader
%div= local_var.method
%div Some other content
%div More content
%div Some footer content

When someone hits my_app.com/controller_name/download.docx, a Word file is generated and downloaded for them.

In order to ensure this happens, I have a route for the download method in my routes.rb file:

resources :model_name do
  member do
    get :download
  end
end

Apologies for the long reply ... this has worked well for me and I hope helps you through this issue!

craig.kaminsky
  • 5,588
  • 28
  • 31
0

So, I figured it out. I added format: 'docx' to the route and it works now.

Note: as @kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem.

Let's create a download logic.

Gemfile

gem 'responders'
gem 'htmltoword', '~> 0.5.1'

routes.rb

get 'download' => 'foos#download', format: 'docx' #added format

foos_controller.rb

class FoosController < ApplicationController
  respond_to :docx

  def download
    @bar = "Lorem Ipsum"

    respond_to do |format|
      format.docx do
        # docx - the docx template that you'll use
        # filename - the name of the created docx file

        render docx: 'download', filename: 'bar.docx'
      end
    end
  end
end

download.docx.erb

<p><%= @bar %></p>

And I've added some link to trigger the download logic:

<%= link_to 'Download bar.docx', foo_download_path %>

Which will download the bar.docx file with "Lorem Ipsum" in it.

Vucko
  • 20,555
  • 10
  • 56
  • 107