← Back to Upcase

Bootstrap Modal Validation/PDF's


(Fritz Rodriguez) #1

I have two questions (dev env - macosx mountain lion, Rails4, Ruby 2.0.0)

1.What gems do you recommend for generating pdf’s and uploading documents, photo’s, etc…

Usage - After the user completes a series of forms, they should get a email with a pdf attachment that includes details of their transaction. Also, the user should be able to upload their avatar and other required documentations. I am guessing it will be paperclip and prawn, but maybe there is something new?

  1. I have a twitter bootstrap modal working correctly except the modal closes when a validation fails, how I can prevent this action before the redirect takes place? I don’t want the new or create actions as the user will only have the ability to update information already in the database.

Here is what I have so far…

	def update
	if @athlete.update(athletes_params)
		redirect_to @athlete, notice: "Your information was updated successfully"
   		mixpanel.track 'User Updated', {
  		:distinct_id => @user.id,
  		:time => @user.updated_at
  		}
  		mixpanel.set ({ :distinct_id => @user.id, :email => @user.email, :first_name => @user.first_name, :last_name => @user.last_name})
	else
		flash[:error] = "Opps! Sorry #{@user.first_name} you forgot something on the, please complete all the required fields"
		@sport = @user.athlete
		render :show
	end
end

Model -

class Athlete < ActiveRecord::Base
extend FriendlyId
#friendly_id :name, use: :slugged

belongs_to :user, dependent: :destroy
has_many :sports

accepts_nested_attributes_for :sports
validates_inclusion_of :ncaa, :in => [true], :message => 'You must certify'
validates_presence_of :campus_box, :message => 'Please enter your campus box'

def parents_name
	"#{parent_guardian_1_first} #{parent_guardian_1_last}" " & " "#{parent_guardian_2_first} #{parent_guardian_2_last}"
end

end

Modal - The form submit action

<%= link_to 'Open NCAA Student-Athlete Statement', ncaa_form_athlete_path(@athlete), :target => "_blank" %>
    <%= simple_form_for @athlete, :url => athlete_path(@athlete), :validate => true, :html => { :method => :put, :multipart => true, :class => 'form-horizontal' } do |f| %>

Thanks for the help!


(Jon Seidel) #2

@fritzrodriguez… I’ve successfully used WickedPDF and the wkhtmltopdf binary on heroku’s Cedar stack using Rails 3.2.13 and Ruby 1.9.3. My main development machine is Kubuntu, although I do carry a MacBook for traveling. I chose to install the binary manually, since many of the gems have outdated binaries.

I blogged about it here and can show you code if you’d like. Using wkhtmltopdf can be a bit tricky because it’s a binary that runs outside of the rails execution environment and therefore doesn’t know about the Rails paths. WickedPDF helps handle that, but it still requires careful setup.

For uploading documents/photos, I’ve used Thoughtbot’s PaperClip gem… really nice.


(Fritz Rodriguez) #3

@JESii Thanks for your reply. Would love to see your configuration.

That being said, what are the advantages of using WickedPDF vs Prawn, just curious to hear your thoughts. In the meantime I will read the blog post…Do you need my email or are you going to post the code here…I am new the forum. Thanks again for your help…appreciated!

I will try out PaperClip for uploading.


(Jon Seidel) #4

No problem; I’m running out to an appointment and will post the basics on the forum once I get back in the office.

I went with wkhtmltopdf because I wanted to be able to just reuse my existing html layout, which is what that combination does. With Prawn, you have to build up your entire PDF document in code. I’ve used both and prefer wkhtmltopdf for when I just want to use the existing layout; Prawn for fancier stuff.


(Fritz Rodriguez) #5

Okay! Sounds good!

Great recommendation on PaperClip…that was the easiest implementation of a rails gem I have experienced so far!


(Jon Seidel) #6

Glad it worked out so well. Here’s my punch list for WickedPDF/htmltopdf…
OK… here’s what I did for WickedPDF/wkhtmltopdf

  • Added gem “wickedpdf” in my Gemfile

  • Copied the appropriate wkhtmltopdf binary into the bin/ directory

  • Updated config/initializers/wicked_pdf.rb to include:

    config.exe_path = "#{Rails.root}/bin/wkhtmltopdf"
    so that it knows where I’ve stored the binary

  • Added the following to the show method in my controller; this is what drives the whole thing:

    format.pdf do

    render :pdf => “AccRpt_#{@accident_report.id}”,

     :template => 'accident_reports/show', :formats => [:pdf], :handlers => [:haml],
    
     :show_as_html => params[:debug].present?,      # allow debuging based on url param
    
     :page_size => :letter,
     #:debug_javascript => true,
     :footer => {
       :left => "#{Time.now}",
       :center => "Accident Report ##{@accident_report.id}",
       :right => "Page [page] of [topage]",
       :line => true
     },
     :user_style_sheet => "/assets/stylesheets/pdf.css",
     :layout => "pdf.html" #false
    

end

  • Created a new stylesheet called pdf.css and a new javascript file called pdf.js. They don’t do a lot, but I pretty much extract out as much stuff as I can so that only what’s absolutely necessary is included
  • Created a new layout called pdf.html.haml. Cuts out all the overhead associated with ‘normal’ html display. Again, nothing but what’s absolutely necessary to produce the PDF.
  • In that layout, used a Content Delivery Network (CDN) for all the jquery stuff I use - that’s directly available to wkhtmltopdf since it’s a full URL (Remember I said that you can’t use relative URL’s with wkhtmltopdf).
  • Where necessary, used the WickedPDF-provided helpers to include other stylesheets/javscript files. Make sure you read the “Usage Conditions - Important!” section - I added that after I ran into problems.
  • Created a show.pdf.haml view template with the data/formatting I wanted.
    NOTE: If you want to render an existing partial, you must add “:handlers => [:haml], :formats => [:pdf]” to the render and then you can actually reuse an html partial, such as _format_body.html.haml. If you don’t do that, you’ll get a complaint that it doesn’t have the proper pdf template.
  • Added ‘PDF’ links at various places, such as in the index view and the show view.
  • Made sure that the PDF mime-type was registered; ‘Mime::Type.register “application/pdf”, :pdf’ in config/initializers/mime_types.rb

I hope I remember everything… and pardon the poor formatting on the code: the code sample feature just wasn’t working.


(Fritz Rodriguez) #7

@JESii Thanks so much! I will try it out tonight and let you know how it went.

Have a good night!


(Jon Seidel) #9

@fritzrodriguez… sorry to hear you had some issues. If you have time, I’d be interested to know what you ran into so that I can update my punch list. Good luck with your development!


(Fritz Rodriguez) #10

@JESii I will give more details later, but I think it had to with the wkhtmltopdf…I downloaded the dmg file and not sure it install correctly (as I did see anything in the /bin dir) and inspecting the package content doesn’t give it me an idea of what needs to go in the /bin directory.

I did the install using the ‘wkhtmltopdf-binary’ gem and was not successful, so I downloaded the dmg file, so maybe that is the issue?

Best


(Jon Seidel) #11

@fritzrodriguea… No worries; do it when you can, but I think you’re right as to the problem.

First off, I also had issues with the wkhtmltopdf-binary gem and gave up on that. Also, you can’t use the .dmg image; you need to wind up with a ‘wkhtmltopdf’ executable binary. Maybe when you install the .dmg file you wind up with such a binary but I don’t know. You might have to do something like:

find / -iname '*wkhtmltopdf*'

to locate the binary, assuming it got placed somewhere. This is kinda brute force, since I don’t know whether it would wind up in somewhere in your home directory (that’s where you eventually want it if you’re going to deploy the app) or in a system directory.

Anyway, glad you got it going with Prawn and were able to move on.


(Fritz Rodriguez) #12

@JESii Update - Using homebrew I was able to download the wkhtmlopdf binary and sym link using the following in the wicked_pdf.rb

WICKED_PDF = {
:exe_path => ‘/usr/local/bin/wkhtmltopdf’
}

Rails4 - Received an error when I set it using config.exe_path, so the above snippet worked. Also, you don’t need to to register the mime-type, it sets automatically.

Issue now has to do with layout and rendering, seems to hang on the request…any chance you can send the sample code in an email or gist? Hope this help update your punch list.


(Fritz Rodriguez) #13

@JESii Last note… With the format do action in the controller, updating a record caused the following error

“ActionController::UnknownFormat - ActionController::UnknownFormat”

Setting the mine type does not resolve the issue. Removing it from the show action fix it…


(Jon Seidel) #14

Thanks very much, @fritzrodriguez… You’re absolutely right on the config.exe_path - I had that wrong.

How much of the sample code would you like? This is part of an application developed for a client so I have some disclosure restrictions. I created a gist with the show action and other related stuff. Not sure if that answers your request, but take a look and let me know what else you’d like to see.

Regarding the request hang, I experienced that as well when getting started; plus, the generation can sometimes take a long time. Did you try adding “?debug=1” to the URL to see if the HTML was properly generated?


(Fritz Rodriguez) #15

@JESii I will debug per your suggestion for the hang issue.

Glad I could help! and thanks for helping me! Do you think it makes a difference where the wkhtmltopdf resides (/bin folder in app or like I have it now as a sym link) in processing the pdf which in turn could be the cause of the hang issue?


(Jon Seidel) #16

No, @fritzrodriguez; that shouldn’t be the issue. wkhtmltopdf can go pretty much anywhere as long as you specify the :exe_path appropriately and your app has access to that directory in test/dev/prod environments. If you’re on Heroku you’re pretty much forced to put in your app’s /bin directory or other local space.