I wrote a Ruby script in which I use Nokogiri.
For Rails I made this module in the lib/ directory:
require "net/http"
require "uri"
require 'nokogiri'
Module gk_CT
class CT
def getCT
uri = URI.parse("http://www.website.com")
CT = Net::HTTP.get_response(uri)
proc = Nokogiri::HTML(CT.body)
CTQ = Array.new
CTQ << proc.css('td')
end
end
In the controller I have:
require 'gk_CT'
def show
#CT= gk_CT::CT.getCT()
respond_to do |format|
format.html # show.html.erb
format.json { render json: #CT}
end
end
It always gives me the error:
cannot load such file -- nokogiri
and I have no idea why.
If the script is part of an actual Rails project, then you need to add Nokogiri to the Gemfile (with the line gem 'nokogiri'). If you're not in a Rails project or aren't using Bundler or some such weird thing, you'll still need to install the gem (gem install nokogiri).
Related
I am trying to install wicked_pdf in order to generate pre-filled contracts between 2 users on my Rails application.
I feel like I have installed wicked_pdf properly, but I get an "ActionController::UnknownFormat" error.
What I did :
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
With or without uncommenting the 'exe...' lines (one after the other), I still get the error :
# initializers/wicked_pdf.rb
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
}
My controller:
#bookings_controller.rb
class BookingsController < ApplicationController
def show
#booking = Booking.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render pdf: "test_wicked_pdf"
end
end
authorize #booking # For Pundit
end
The render HTML is working when i go to localhost:3000/bookings/135 ...
# bookings/show.html.erb
<h1>PDF test</h1>
...but not the PDF when I comment out "# format.html" in my controller
# bookings/show.pdf.erb
<h1>PDF test</h1>
Thanks a lot in advance
I know it is a pretty old question but you can use the gem wkhtmltopdf-binary-edge instead of the regular wkhtmltopdf-binary gem. It worked for me.
I'm using Nokogiri to create XML:
class Output
require 'nokogiri'
def self.xml
data = IncomingData.all
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
data.each do |o|
xml.id {
xml.account_id_ o.account_id
xml.date_ o.date
xml.account_type o.account_type
xml.activity o.activity
xml.position o.position
xml.security o.security
}
end
}
end
builder.to_xml
end
end
This is my controller action:
respond_to do |format|
format.xml { render xml: Output.xml }
end
which works nicely:
But I need to output it with indentation like:
account_id
date
account_type
How I can do it?
ruby -v returns:
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin14.0]
nokogiri --version returns:
# Nokogiri (1.6.7)
Just create valid hash, and add to him .to_xml method
I took the approach of Railscast episode 153 revised.
My controller is
class AdminsController < ApplicationController
def index
#examples = Example.all
respond_to do |format|
format.html
format.csv { send_data #examples.to_csv }
format.xls { send_data #examples.to_csv }
format.pdf do
pdf = DownloadPdf.new(#examples)
send_data pdf.render, filename: 'generate_table.pdf',
type: 'application/pdf', disposition: "inline"
end
end
end
end
and my download_pdf.rb file is
class DownloadPdf < Prawn::Document#make_table
require 'prawn/table'
def initialize(example)
super()
#examples = example
line_items
end
def line_items
image "#{Rails.root}/app/assets/images/logo.png"
table [[1,2],[3,4]]
end
end
I am using gems
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2'
gem 'prawn-table', '~> 0.1.0'
Thanx in advance for helping.
TL;DR: Update prawn gem by adding this to your Gemfile: gem 'prawn' and running bundle install.
Longer answer: You are using an old version of Prawn - that ref you are using in your Gemfile refers to somewhere in 2013. prawn-table 0.1 is newer and requires a newer version of prawn. More precisely, it's using Prawn's ::FLOAT_PRECISION constant, which was added in this 2014's commit to Prawn.
Please use below on Gemfile
gem 'prawn'
and then remove Gemfile.lock
and then
bundle install
restart server
you can use round(2) like this..
val= 456.7890999999
val.round(2)
and Ans will be 456.79
I'm using PDFKit with it's Middlware to render HTML as PDF but it keeps having a TypeError when I try to go to localhost:3000/booklets/1.pdf
can't convert Hash into String
It says the error is in BookletsController#show. This is an excerpt from my booklets_controller.rb
def show
#booklet = Booklet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:action => "show.html.erb", :formats => [:html])
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "booklet.pdf", :type => 'application/pdf')
return
}
end
end
environnment.rb
# Initialize the rails application
Ziin::Application.initialize!
Mime::Type.register "application/pdf", :pdf
excerpt from application.rb
require 'pdfkit'
config.middleware.use "PDFKit::Middleware"
PDFKit.configure do |config|
config.wkhtmltopdf = { :exe_path => '/usr/local/bin/wkhtmltopdf' }
end
The problem is the exec path you specify. I just replicated the problem on my own machine (ubuntu 12.04), and I fixed it by NOT using the gem install of wkhtmltopdf, which adds some version of wkhtmltopdf that doesn't work.
1.) Look here for how to install wkhtmltopdf (non-gem version)
https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF
2.) Remove 'wkhtmltopdf' from your gem file
3.) Find the wkhtmltopdf executable by doing 'which wkhtmltopdf'. Use the path that is returned in the exe_path declaratioin.
4.) Don't know if this next step matters, but I moved:
PDFKit.configure do |config|
config.wkhtmltopdf = { :exe_path => "#{PATH}"
end
into config/initializers/pdfkit.rb
also remove the gem by 'gem uninstall wkhtmltopdf'
Trying to get rails debugger work. I did following:
gem install debugger
Added in Gemfile
gem "debugger", "~> 1.2.0"
bundle install - no error
Now I put debugger in one of my controllers
def show
require 'debugger'; debugger
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #user }
end
end
When I point my browser, I get following error on browser
LoadError in UsersController#show
cannot load such file -- debugger
I am using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] and Rails 3.2.6 with Phusion passenger
Any idea what is wrong?
Remove require statement and start rails server with debugger option
rails server --debugger
Not exactly sure how you want to use the debugger gem, but it is great for creating stop-points in active code. In Rails 3, you only need in your routes.rb:
gem 'debugger'
It doesn't have any effect in production and to have it stop your code at any point, simply put 'debugger' and your server console will give you access to that point in your code. At that point, to check the status of variables, you need to access IRB, so just type in 'irb'. In your 'show' action, you can check the value of your params and whether #user would be populated.
def show
debugger
#user = User.find(params[:id])
...