ruby-docx / docx

a ruby library/gem for interacting with .docx files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not gettin docx class working properly on rails

brpl20 opened this issue · comments

Describe the bug

I'm trying to create a .docx template using rails but I'm getting problem in loading the 'docx' class.

Repository

NoMethodError in ClientsController#create
`undefined method `close' for nil:NilClass`

Image

Controller

  def templater
    require "docx"
    doc = Docx::Document.open("base.docx")
      doc.paragraphs.each do |p|
        p.each_text_run do |tr|
          tr.substitute('_placeholder', 'teste')
        end
      end
      doc.save('base-edit.docx')
  end

Gemfile
gem 'docx', :require => ["docx"]

Environment

  • Ruby version: [e.g 2.6.5]
  • docx gem version: generic gem docx (last version)
  • OS: [e.g. Ubuntu]
  • gem 'rails', '~> 5.2.4', '>= 5.2.4.1'

I made a Pull Request in your Rails repo: https://github.com/pellibr/prc3/pull/1

The problem is not in the gem. The problem is in our code. Check my Pul Request: https://github.com/pellibr/prc3/pull/1

Hello! THanks a lot and sorry for bother with this question, will study more about rails and dealing with files.

Best regards !

Can someone help me with this issue? i am facing the same and the pull request is not avaialable. @WaKeMaTTa @brpl20

@Manshi19 sorry, I don't remember my fix. Share your code.

heyyy, sorry, Ive lost that repo, but it was about the rails root folder

when you going go save your edited document with the gem you have to use like this:

doc_to_save = doc.save(Rails.root.join("tmp/example.docx").to_s)

please check it out how are you doing