karnov / htmltoword

Ruby html to word gem
MIT License
177 stars 70 forks source link

ActionController::UnknownFormat #47

Open ghost opened 8 years ago

ghost commented 8 years ago

Hi,

I am unable to view the docx file, as every time I go and try accessing the routes, it throws error

ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/enterprisecycles_controller.rb:22:in `testcycle_requirement_document'

respond_to do |format| format.docx do

I have written following code:

respond_to :docx

def testcycle_requirement_document
  respond_to do |format|
    format.docx do
      render docx: 'requirement_document', filename: 'my_file.docx'
    end
  end
end

I am using htmltoword (0.5.1)

Kindly help.

lwnhp commented 8 years ago

I'm experiencing the exact same issue. Any clues so far?

adelivuk-zz commented 8 years ago

I had the same issue and I've posted this question on SO - [RoR HTML template to .docx](http://stackoverflow.com/questions/37161214/ror-html-template-to-docx/37165372], and I've managed to solve it. From SO).

Note for RoR versions 4.2. : respond_with / Class-Level respond_to has been removed to an individual gem, so you need to install 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

_fooscontroller.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.

rvanrinsum commented 8 years ago

Had the same problem. I wrote the line in routes.rb like this:

routes.rb

get :export, defaults: { format: :docx }

and I didn't need the responders gem. also, I didn't need the respond_to :docx in the controller

lukelex commented 6 years ago

@ghost does @rvanrinsum's solution work for you?