Unknown format with wicked_pdf - ruby-on-rails

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.

Related

Rails respond with paperclip image.url multiple URLs

I'm building a simple rails application where users can upload some images. Now I need a simple controller that returns the image's URLs of related record which have the same id_attivita. To do so I create a function in the controller and enable it in the routes.rb file.
My question is about how to respond to the http request with the attribute value of image.url provided from paperclip?
def getattached
#photo_attivitum = PhotoAttivitum.where(id_attivita: params[:id_attivita])
respond_to do |format|
#photo_attivitum.each do |p|
format.html { render :json => p.image.url}
end
end
end
it works but it returns only the URLs of the first record not the other four record's URLs...
How can I do this?
Add the following gem to your Gemfile
gem 'active_model_serializers'
Then install it using bundle
bundle install
You can generate a serializer as follows
rails g serializer photo_attivitum
it will create Serializer class file in
# app/serializers/photo_attivitum_serializer.rb
class PhotoAttivitumSerializer < ActiveModel::Serializer
attributes :id, :image_url
def image_url
object.image.url
end
end
And in controller
def getattached
#photo_attivitum = PhotoAttivitum.where(id_attivita:
params[:id_attivita])
render json: #photo_attivitum
end
Not sure what u want to do? Show all image urls in json?
urls = #photo_attivitum.pluck('image_url')
format.html { render :json => urls}

RoR HTML template to .docx

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.
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?
Have you tried caracal-rails? You can find it here
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!
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.

uninitialized constant Prawn::FLOAT_PRECISION error showing while using prawn

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

Rails: render doesn't work, still get `Template is missing`

I am currently learning Rails Guides. I went through the steps but still encountered a mistake.
My Ruby version is ruby 2.1.1p76 and the Rails version is 4.0.4.
As the guide directed, I created an Article Controller.
class ArticlesController < ApplicationController
def new
end
def create
render plain: params[:article].inspect
end
end
I should get {"title"=>"First article!", "text"=>"This is my first article."} but the output turned out to be
Template is missing
Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.`
Here is my related routes:
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
Update: render plain: is a new method introduced in Rails 4.1.0 referred to this issue.
In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.
Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text
Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:
render plain: params[:article].inspect
With your current version of Rails 4.0.4, you can go for:
render text: params[:article].inspect
If you want to see textual information of params[:article] on your page then you can use render text
try this
class ArticlesController < ApplicationController
def new
end
def create
render text: params[:article].inspect
end
end
You will get
{"title"=>"First article!", "text"=>"This is my first article."}
# i.e. your params(whatever params hash contains)
Change Rails version in your Gemfile:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
Then run:
bundle install
Make sure that your Rails version now is > 4.1
You no need template means you could use render nothing: true
Try like this:
class ArticlesController < ApplicationController
def new
end
def create
params[:article].inspect
render nothing: true
end
end
Please refer this link click here
You may want to read through the following documentation.
Rendering pure text is most useful when you're responding to Ajax or
web service requests that are expecting something other than proper
HTML.

cannot load such file - Nokogiri rails

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

Resources