wicked-pdf: Error: PDF could not be generated - ruby-on-rails-3

I am trying to create pdf order receipts on our app (on Heroku). I am running the PDF generator as a delayed job.
I have:
-created a bin folder in the root of the app
-added the binaries (x86-64) to that folder
-added wicked pdf gem to my gemfile
I am testing it on our staging app.
This is the error I'm getting:
Error: Failed to execute:
["/app/vendor/bundle/bin/wkhtmltopdf", "-q", "--page-size", "Letter", "file:///tmp/wicked_pdf20141022-213-3dpi58.html", "/tmp/wicked_pdf_generated_file20141022-213-1tqzjod.pdf"]
Error: PDF could not be generated!
Command Error: /app/vendor/bundle/ruby/2.0.0/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in 'block in replace_bin_path': undefined method name' for nil:NilClass (NoMethodError)
from /app/vendor/bundle/bin/wkhtmltopdf:16:in
Any ideas on what may be going on here? I'm at bit of a loss.
CODE:
#config/initializers/wicked-pdf.rb
WickedPdf.config do |config|
if Rails.env == 'production' then
config.exe_path = Rails.root.to_s + "/bin/wkhtmltopdf-amd64"
else
if /darwin/ =~ RUBY_PLATFORM then
config.exe_path = '/usr/local/bin/wkhtmltopdf'
else
raise "UnableToLocateWkhtmltopdf"
end
end
end
I'm calling to queue the job in my order controller
#jobs/pdf_receipt_job
class PdfReceiptJob < Struct.new(:order_id)
def perform
order = Order.find(order_id)
# create an instance of ActionView, so we can use the render method outside of a controller
view = ActionView::Base.new(ActionController::Base.view_paths, {})
html_template = view.render(file: "order/receipt_attachment.html.erb", locals:{order:order})
# use wicked_pdf gem to create PDF from the HTML receipt template
pdf_receipt = WickedPdf.new.pdf_from_string(html_template, :page_size => 'Letter')
# save PDF to disk. Later, to be stored in fog, sent to S3, than saved back to our DB
pdf_path = Rails.root.join("tmp", "marmoset-receipt#{order.id}.pdf") #ex: will be at tmp/marmoset-receipt215.pdf
File.open(pdf_path, 'wb') do |file|
file << pdf_receipt
end
end
end
The receipt template (to become PDF receipt):
#views/order/receipt_attachment.html.erb
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title>Order Receipt from Marmoset</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="width: 75%">
<header>
<div style="display: inline; width:100%">
<img src="http://stillmotionblog.com/wp-content/uploads/2013/08/RGB-marmoset-seal-purple.png" alt="Marmoset" style="height: 200px; padding:20; float:left">
<h3 style="font-family: Helvetica; float:right; color:gray;">Order Number: <%= order.id %></br>
Charge Key: <%= order.stripe_charge_key %>
</h3>
</div>
</header>
<div style="margin: 0 auto">
<table cellpadding="5" cellspacing="2" summary="Order Receipt" width="100%" style="font-family: Georgia;">
<thead>
<tr>
<th bgcolor="#8A4FAB" style="color:white" scope="col">Track</th>
<th bgcolor="#8A4FAB" style="color:white" scope="col">License</th>
<th bgcolor="#8A4FAB" style="color:white" scope="col">Price</th>
</tr>
</thead>
<tbody style="font-family: Georgia;">
<% order.line_items.each do |line_item| %>
<tr>
<td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= line_item.track.display_name %></td>
<td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= line_item.license.display_name %></td>
<td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency line_item.price %></td>
</tr>
<% end %>
<tr>
<td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C">Subtotal</td>
<td style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency order.price %></td>
</tr>
<% if order.discount > 0 %>
<tr>
<td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C">Discount</td>
<td style="font-family: Georgia; color: #5C5C5C">-<%= number_to_currency order.discount %></td>
</tr>
<% end %>
<tr>
<td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C ">Total</td>
<td style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency order.total %></td>
</tr>
</tbody>
</table>
</div>
<hr style="margin: 30px 0px 30px 0px">
<p style="font-family: Georgia; color: #5C5C5C">Licensing information for your order:</p>
<ul style="font-family: Georgia; color: #5C5C5C">
<% order.line_items.map{|l| l.license}.uniq.each do |license| %>
<li><strong><%= license.name %> <%= "(#{license.range.html_safe})" if license.range.present? %>:</strong>
<ul>
<% if license.permitted_content.present? %>
<li>
<b>Permitted Content:</b>
<%= license.permitted_content.html_safe %>
</li>
<% end %>
<% if license.non_permitted_content.present? %>
<li>
<b>Non-Permitted Content:</b>
<%= license.non_permitted_content.html_safe %>
</li>
<% end %>
<% if license.number_of_uses.present? %>
<li>
<b>Number of Uses:</b>
<%= license.number_of_uses %>
</li>
<% end %>
<% if license.distribution.present? %>
<li>
<b>Distribution:</b>
<%= license.distribution %>
</li>
<% end %>
<% if license.term.present? %>
<li>
<b>Lifespan:</b>
<%= license.term %>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
<p style="font-family: Georgia; font-size: .9em; color:gray; font-style:italic; padding: 5px; margin-top:30px; text-align: center">
Thank you for supporting our family of musicians with your purchase. Your purchase makes an impact on the lives of hard working, blue collar artists. We hope you enjoy the music and we can't wait to see what stories you tell. Give us a shout at <a style="text-decoration: none" href="mailto:compass#marmosetmusic.com" target="_blank"><span style="color:#8A4FAB; font-style: normal">compass#marmosetmusic.com</span></a> to share your masterpiece when it's finished.</p>
</div>
</body>
</html>
EDIT: if I run wkhtmltopdf www.marmosetmusic.com myhomepage.pdf in my terminal, it works locally, if I log into the bash of my heroku app and run this, I get the same error /app/vendor/bundle/ruby/2.0.0/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in 'block in replace_bin_path': undefined method 'name' for nil:NilClass (NoMethodError)
from /app/vendor/bundle/bin/wkhtmltopdf:16:in'`

Related

How to solve the SyntaxError: Unexpected identifier problem occurring during compilation of ExpressJS (ejs) file?

<% include partials/header.ejs %>
<div class="table-wrapper">
<% if (players.length > 0) {%>
<table class="table table-hovered">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Image</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Position</th>
<th scope="col">Number</th>
<th scope="col">Username</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<% players.forEach((player, index) => { %>
<tr>
<th scope="row"><%= player.id %></th>
<td><img src="/assets/img/<%= player.image %>" class="rounded-circle player-img" alt=""></td>
<td><%= player.first_name %></td>
<td><%= player.last_name %></td>
<td><%= player.position %></td>
<td><%= player.number %></td>
<td>#<%= player.user_name %></td>
<td>
Edit
Delete
</td>
</tr>
<% }) %>
</tbody>
</table>
<% } else { %>
<p class="text-center">No players found. Go <a href="/add" >here</a> to add players.</p>
<% } %>
</div>
</div>
</body>
</html>
This is the error being displayed after running the .ejs file from terminal,
SyntaxError: Unexpected identifier in C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\views\index.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
at new Function ()
at Template.compile (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:626:12)
at Object.compile (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:366:16)
at handleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:459:10)
at View.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\response.js:1012:7)
I am new to expressjs and this a code from a demo app in GitHub.
It would be a great help if this problem is solved.
Read this document https://github.com/tj/ejs and i think your foreach loop having some problem check this first and then try once.
<% for(var i=0; i<players.length;i++) { %>
<tr id="idrow_<%= players[i]._id%>">
<td id="id">
<%= players[i]._id%>
</td>
<td id="name">
<%= players[i].first_name%>
</td>
<td id="phone">
<%= players[i].last_name%>
</td>
<td id="email">
<%= players[i].position%>
</td>
<td id="vehicle">
<%= players[i].number%>
</td>
<td id="other">
<%= players[i].user_name%>
</td>
<td><button type="button" class="btn btn-secondary" id="edtbtn" onclick="edituser('<%= players[i]._id %>')" name="edit"><b>edit</b></button></td>
<td><button type="button" class="btn btn-secondary" id="delbtn" onclick="deleteuser('<%= players[i]._id %>')" name="delete">
<span><strong>Delete</strong></span>
</button>
</td>
</tr>
<% } %>
========================================================================

How can I render these Bootstrap 3 panels inline?

Here's the Bootply
How can i render these panels tp be inline? I don't see what I'm missing here, i have a container, I have a row, I have the col-md-4 nested within the row?
For context, each of those panels is written in a '_quotes.html.erb' partial, which is rendered by a Quotes.all.each block within the 'index.html.erb' view.
index.html.erb
<div class="container">
<div class="row">
<% #quotes.each do |quote| %>
<%= render :partial => "quote", locals: {quote: quote} %>
<% end %>
</div>
_quote.html.erb
<% #quote = local_assigns[:quote] %>
<%= link_to quote_path(#quote) do %>
<section id="quotes">
<!-- <div class="container"> -->
<div class="col-md-4 col-md-4 col-md-4 text-center">
<div class="panel panel-success panel-quote link-panel">
<div class="panel-heading">
<strong>GLA</strong>
</div>
<div class="panel-body">
<p><strong>Quote ID; <%= #quote.id %></strong></p>
</div>
<table class="table table-bordered">
<tr>
<td>Company name</td>
<td><%= #quote.co_name %></td>
</tr>
<tr>
<td>Company number</td>
<td><%= #quote.co_number %></td>
</tr>
<tr>
<td>Office postcode</td>
<td><%= #quote.postcode %></td>
</tr>
<tr>
<td>Industry</td>
<td><%= #quote.industry %></td>
</tr>
<tr>
<td>Previous cover</td>
<td><%= #quote.prev_cover %></td>
</tr>
<tr>
<td>Lives overseas</td>
<td><%= #quote.lives_overseas %></td>
</tr>
<tr>
<td>Scheme start date</td>
<td><%= #quote.scheme_start_date %></td>
</tr>
<tr>
<td>Payment frequency</td>
<td><%= #quote.payment_frequency %></td>
</tr>
<tr>
<td>Commission level</td>
<td><%= #quote.commission_level %></td>
</tr>
</table>
</div>
</div>
<!-- </div> -->
</section>
</div>
<% end %>
There's a couple of mistakes
You have only a single col-md-4 which means all children will fit to that single column.
You have another container div inside your col-md-4 which basicly resets the whole thing and again a single col-md-4 in there.
Edited Bootply

Logged out users are seeing error message "undefined method `leads' for nil:NilClass"

I made a simple app that lets users track their sales leads. Everything works except for the home page for logged out users, who get the error message - undefined method `leads' for nil:NilClass.
The index in the leads controller:
def index
#leads = current_user.leads.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end
The homepage:
<% if user_signed_in? %>
<div class="row">
<div class="span3">
<h2>Leads</h2>
</div>
<div class="pull-right">
<%= link_to 'Add New Lead', new_lead_path, class: 'btn btn-primary' %>
</div>
</div>
<div>
<table id="leads" class="display">
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Phone</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Website</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Email</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Notes</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Last Updated</th>
</tr>
</thead>
<tbody>
<% #leads.each do |lead| %>
<tr>
<td><%= link_to lead.company_name, lead %></td>
<td><%= lead.contact %></td>
<td><%= lead.phone %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.website %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.email %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.notes %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.updated_at.strftime("%d %b %y") %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="hero-unit">
<h1>Welcome to SnapLeads</h1>
<p>
The simplest CRM ever.
</p>
<p>
<%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
</p>
</div>
<% end %>
Why would the page be trying to access the leads method if the user is logged out?
Because the code in your index action is always executed. You could use something like the following in your controller:
def index
#leads = current_user ? current_user.leads.all : nil
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end

Rails submit button not working in Internet Explorer

Bizzarre issue I've been working that is beyond frustrating. The submit button on one of my edit views is not working in Internet Explorer. It is a pretty standard layout using input type = submit (not a customized button_to or something).
The button works fine in Safari, Chrome, and Firefox on both Mac and Windows.
Edit submit buttons work fine on other pages in IE
Using nested_form_for, with :html => {:multipart => true}
Submit button
<%= f.submit("update", :class=>"post_button")%>
Has anyone had issues with submit buttons not working in IE?
== update ==
Updated jQuery to make sure it wasn't a nested_form issue. I actually think the link may be not been working before I added the nested form. However, it has always been a multipart form.
View
<head>
<%= stylesheet_link_tag 'profile' %>
<%= javascript_include_tag "jquery.min", "nested_form" %>
</head>
<%= nested_form_for(#profile, :html => {:multipart => true}) do |f| %>
<div class="content">
<div class="editprofilesub">
<div class="profile_title"> basic </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :firstname, "First Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :firstname, :class => "profilefield", :class=>"profilefield" %>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :lastname, "Last Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :lastname, :class => "profilefield" %>
</td>
</tr>
<td class="profileformright">
<%= f.label :avatar, "User Image" %><br>
</td>
<td class="profileformleft">
<i>Please rotate images prior to uploading.</i> <br>
<%= f.file_field :avatar%>
</td>
</table>
<div class="profile_title"> location </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :city %>
</td>
<td class="profileformleft">
<%= f.text_field :city, :class => "profilefield" %>
</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="profileformright">
<%= f.label :state, "State" %>
</td>
<td class="profileformleft">
<%= f.select :state, Carmen.state_names(),{}, :class=> "state" %>
</tr>
</table>
<div class="profile_title"> about </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :bio, "about" %>
</td>
<td class="profileformleft">
<%= f.text_area :bio, :rows => '5', :cols => '60', :class => "profilefield" %><br>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :dob, "Date of Birth" %>
</td>
<td class="profileformleft">
<%= f.date_select :dob,
{:start_year => Time.now.year,
:end_year => 1900,
:order => [:month, :day, :year]}%>
</td>
</tr>
</table>
</div>
<div class="right">
<%= f.submit("update", :class=>"post_button")%>
</div>
</div>
<%end%>
I just found a similar issue in my non-MVC application, in IE8 and below. When enter is used, the server-side click handler for the submit button wasn't being executed. The problem was that IE doesn't POST the submit button when enter is used (but does when it is clicked). So, .NET knows it's a post back, but not what control caused it.

Rails create and update model Cant resolve paths

I cant understand why edit and new links doesn't work. I have controller named CargoController and have model Car.
CargoController is:
def new_auto
end
def edit_auto
end
def index
#cars=Car.find_all_by_UserId(session[:user_id])
if #cars.nil?
end
end
Car model is:
class Car < ActiveRecord::Base
validates_presence_of :TransportTypeId
validates_presence_of :CarModelId
set_primary_key :CarId
has_one :TransportType
has_one :CarModel
belongs_to :User
has_many :PropertyOfCar, :dependent => :destroy
has_many :CarProperty, :through => :PropertyOfCar
end
The View CargoController index.erb is:
<%if !#cars.blank?%>
<table width="100%" border="0" cellpadding="0px" cellspacing=1px">
<% #cars.each do |car| %>
<tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<table border="0" width="100%" style="color:black">
<tr>
<td width="15%" align="left" valign="top" >Тип ТС:</td>
<td width="70%" align="left" valign="top"><%= TransportType.find_by_TransportTypeId(car.TransportTypeId).Name %></td>
<td align="right" valign="top" rowspan="2">
<img alt="" src="/images/Truck-icon.png"/>
</td>
</tr>
<tr>
<td width="15%" align="left" valign="top">Car model:</td>
<td width="70%" align="left" valign="top"><%= CarModel.find_by_CarModelId(car.CarModelId).ModelName %></td>
</tr>
<tr>
<td align="center" valign="top" colspan="3">
<label class="Chars"> Properties</label>
</td>
</tr>
<tr colspan="3">
<td>
<%= button_to 'Edit', :controller=>:cargo,:action=>:edit_auto,:id=>car.CarId %>
</td>
</tr>
</table>
</td>
</tr>
<% end %>
</table>
<% end %>
the view edit and new is :
<div class="center">
<%= render 'form' %>
</div>
The template _form is:
<%= form_for(#car) do |form| %>
<table >
<tr>
<td>
<label for="transport_type">Car Type:</label>
</td>
<td>
<%= form.select(:TransportTypeId, TransportType.all.collect {|p| [ p.Name, p.TransportTypeId ] }, { :include_blank => 'Select type'},{:style=>'width: 200px'}) %>
</td>
</tr>
<tr>
<td>
<label for="transport_type">Car model:</label>
</td>
<td>
<%= form.select(:CarModelId, CarModel.all.collect {|p| [ p.ModelName, p.CarModelId ] }, { :include_blank => 'Select model' },{:style=>'width: 200px'}) %>
</td>
</tr>
<tr>
<td colspan="2">
<%= button_to "add property",{:controller=>:cargo,:action=>:new_property},{:class =>"Button_style"} %>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<%= form.submit "Add car", :class => "submit" ,:class =>"Button_style"%>
</td>
</tr>
</table>
<% end %>
So then i try to add or update car rails ask a car controller or write
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: <%= form_for(#car) do |form| %>
2: <table >
3: <tr>
4: <td>.
What shall i do to make it work?
In a routes i have
get "cargo/index"
get "cargo/new_auto"
match ':controller(/:action(/:id))'
I think that you don't create instance variables for each actions.
def new_auto
#car = Car.new
end
def edit_auto
#car = Car.find(params[:id])
end
And you should write "resources :cars" routes.rb as follwing:
resources :cars
I think that you should be able to see the new_auto page.