Rails Prawn UnknownFormat (ActionController::UnknownFormat): - ruby-on-rails

I am new to rails and I am trying to use Prawm to generate pdf's. I am getting this error:
ActionController::UnknownFormat
Here is the code:
def show_deposited_checks
deposit_id = params[:format]
if deposit_id.present?
#payments = Payment.where(:deposit_id => deposit_id)
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
else
#payments = Payment.all.limit(10)
end
end
I an redirecting from here:
redirect_to show_deposited_checks_payments_path(deposit)
I have another method that works just fine so I suspect it has something to do with the redirect and the way the controller is receiving the data. Any help with be greatly appreciated.

I think the problem is that where doesn't execute immediately. There's probably a better way(I'm fairly new to Rails too), but I think this should work:
#payments = Payment.where(:deposit_id => deposit_id).take(10) #or how ever many you want included

Related

How to convert existing html file to pdfs in rails 4?

I have an html file generate_invoice.html.erb. I have to provide a button on the same file to convert generate_invoice.html.erb into a downloadable pdf.I have tried using wicked pdf gem and gem 'wkhtmltopdf-binary'
then I also created the config/initializers/wicked_pdf.rb and set the mime types to pdf
my routes is as follows:
match 'orders/:id/invoice' => 'orders#order_invoice', as:
'order_invoice', via: [:put,:post,:get]
My order_controller is as follows
def order_invoice
#base_url = ENV['base_url']
#order = Order.includes(:status, :user, payment:
[:status]).where(id: params[:id]).first
if (['Notary', 'Attestation','Franking'].include?
#order.service.name)
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i
else
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i + 1
end
render 'order_mailer/generate_invoice',layout: false
respond_to do |format|
format.pdf do
render pdf: "order_invoice"
end
end
end
it however throws this error
ActionController::UnknownFormat at /admin/orders/131/invoice
ActionController::UnknownFormat
basically it is pointing at respond_to do |format| line
What am I doing wrong.Please help me rectify this.Also please let me know the next course of action to achieve the pdf format
I assume your order_invoice file has a format of order_invoice.html.erb instead of order_invoice.pdf.erb which should be the correct format.
respond_to do |format|
format.html
format.pdf do
render pdf: "order_invoice"
end
end
and I hope you already have the view file "order_invoice.html.erb"

how do you get paperclip url to json in rails?

I'm aware of the other questions pertaining to this subject, but none seem to help. I want the paperclip image url passed to json so I can render it in a reactjs component. I obviously cannot use Rails' image_tag helper.
I've defined this method in my items model
def image_url
image.url(:thumb)
end
And this in my controller
def index
#items = Item.all
render :json => #items.to_json(:methods => [:image_url])
end
But literally all that does is replace the rendered page with json. How should I go about this? It doesn't make sense to create a migration and model validation specifically for the image url.
Just needed to format the html.
respond_to do |format|
format.html
format.json { render :json => #items.to_json(:methods => [:image_url]) }
end
Still doesn't solve the part where I'm trying to use that method in a reactjs component, but that's another issue.

Prawn using a pdf template

I want to generate a document with prawn that uses a PDF template.
I get no errors, but the template is completely ignored. Does anyone know why this might be? I would really appreciate any help!
class JobPdf < Prawn::Document
def initialize(job)
super()
#job = job
text #job.title
text #job.target_text
end
def render(job)
doc = Prawn::Document.new({template: "#{Rails.root}/app/templates/jobs/layout.pdf"})
doc.text job.title
doc.text job.target_text
doc.render
end
end
Jobs Controller:
def show
#job = Job.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = JobPdf.new(#job)
send_data pdf.render(#job),
filename: "job#{#job.id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
Prawn has removed templates from its core apparently it is possible to use https://github.com/prawnpdf/prawn-templates to still utilize them but note even prawn states that this is "very buggy"
There is a tutorial, to perform such a task:
http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/
But in order to make it work you have to use PDFtk besides Rails.

Rails 406 for .js.erb

I know this has been asked a lot, but I've looked at all the simple solutions and they're not working.
Firstly, .js.erb responses are working for all my other controllers...I generated a new controller and it won't respond to .js.erb
def report
#report = Report.all
respond_to do |format|
format.js do
render :content_type => 'text/javascript'
end
end
end
This route does work and will render html. I have no idea why .js.erb would work for my other controllers and not for a new one. I've done plenty of fiddling before making this new one...so debugging will be hard for me.
Remove the entire respond_to block. Rails will pick the correct view:
def report
#report = Report.all
end
If you are getting a 406
You need to explicitly disable erb's layout template checking.
render layout: false
You can keep respond_to and format if you need them for supporting other formats.
(NOTE: dynamic js for Content Security Policy purposes?)

How to get my rails pages to return as html?

I've written a rails site in the latest version of rails, based on knowledge of rails from a couple of years ago, and I've hit a horrible snag.
I foolishly decided to ignore the new RESTful routing system and hope for the best.
So all of my views are plain .erb, NOT html.erb
my routes file looks like this
map.connect '/crm/:action/:id', :controller => "contacts", :format => 'html'
here is an example of a method:
def update_emails
Com.update_emails
respond_to do |format|
format.html {redirect_to(:action => 'list')}
end
end
when it redirects to the 'list' action, I get a plain text file that my browser tries to download, instead of the html version of the page that I want.
Is there a simple way for me to tell rails to only send html format files?
Thank you!
EDIT:
list action
def list
if params[:search]
#contacts = Contact.search(params)
else
#contacts = Contact.find(:all, :order => "updated_at desc")
end
end
and the view is a plain .erb file (problem is the same when I make it a .html.erb file)
Also, the same thing happens when I redirect_to other actions
You should use respond_to.
def update_emails
Com.update_emails
redirect_to(:action => 'list')
end
and then in the 'list' action
def list
#some code here
respond_to |format| do
format.html {render :list}
end
end

Resources