Rails Prawn converting my index table into a pdf - ruby-on-rails-3

I am trying to create a .pdf that lists all of my projects(#index).
I found a great link-How do generate PDFs in Rails with Prawn, however it was from 2008 and wanted me to use the prawnto plugin.
I am using Rails 3.2.13 so I decided to use the gem prawn and RailsCast #153 PDFs with Prawn (revised), for reference. I was able to successfully get Prawn working in my:
projects_controller
def show
I am having trouble getting the .pdfs working in my def index though.
I tried to just mimic what I did, using the tutoiral for def show, for def index but am getting a routing error.
Here is my code thus far:
Gemfile
gem 'prawn', '0.12.0'
projects_controller.rb
class ProjectsController < ApplicationController
def index
redirect_to action: :active, search =>params[:search]
end
def active
#action = "active"
....
.... // search code
.... // kaminari code
#projects = Project.order(sort_column + "" + sort_direction)
respond_to do |format|
format.json { render "index" }
format.html { render "index" }
format.pdf do
pdf = ProjectAllPdf.new(#projects)
send_data pdf.render, filename: "project_#{#project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
def show
#project = Project.find(params[:id])
respond_to do |format|
format.json { render json:#project }
format.html # show.html.erb
format.pdf do
pdf = ProjectPdf.new(#project)
send_data pdf.render, filename: "project_#{#project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
end
show.html.erb
<p><%= link_to "Printable Receipt (PDF)", project_path(#project, format: "pdf") %></p>
index.html.erb
<p><%= link_to "Printable Receipt (PDF)", projects_path(#projects, format: "pdf") %></p>
I then formatted my file
project_pdf.rb
class ProjectPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
#project = project
overview_print
end
def overview_print
text "Project #{#project.product}", size: 24, style: :bold, align: :center
move_down 30
text "<b>Product:</b> #{#project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{#project.version_number}", :inline_format => true
move_down 8
....
....
end
end
I then tried to mimic the last file to get #index working
projectall_pdf.rb
class ProjectAllPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
#project = project
overview_print
end
def overview_print
#projects.each do |project|
text "<b>Product:</b> #{#project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{#project.version_number}", :inline_format => true
move_down 8
....
....
end
end
end
Everything works great for #show. I just obviously have gotten myself mixed up on how to do the #index portions (def active, linking the .pdf in index.html.erb and projectall_pdf.rb)

I thought I would post an answer to my question, hopefully it helps somebody.
I actually went ahead and used the 'gem prawnto_2', :require => "prawnto"
It allowed me to use the prawnto and the prawnto tutorial with Rails 3.2
I then just created (method).pdf.prawn pages in my app/views/projects folder.
Then just add your custom pdf code to have you want to layout your pdf views.

Related

Selecting months during PDF export with form submit.

I have a Rails 3.2 application with a model called Mileages. I have added a PDF output of the mileages index action using Prawn PDF.
The mileages_controller's index action looks like this:
def index
#mileages = Mileage.find_all_by_user_id(current_user.id)
#mileages_months = Mileage.find_all_by_user_id(current_user.id).group_by { |t| t.created_at.beginning_of_month }
respond_to do |format|
format.html # index.html.erb
format.json { render json: #mileages }
format.pdf do
pdf = MileagePdf.new(#mileages, view_context)
send_data pdf.render, filename: "mileages_report.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
What I would like to do, is have a link on the index view to the PDF version but alongside would be a start date and an end date. The PDF would then only show the mileage entries between those two dates.
The PDF link would simply be <%= link_to "Mileage Report (PDF)", mileages_path(#mileages, format: "pdf") %>
The activerecord filter would, presumably, be something like this:
#mileages = Milage.where(:date => ??..??)
I am unsure how to tie the above to the date inputs. Any help would be appreciated!
Try to have a kind of form submit since you have dates.
<%= form_tag(mileages_path(format: :pdf), method: :get) do -%>
<%= date_select("mileage_start", "date", order: [:year,:month,:day])%>
<%= date_select("mileage_end", "date", order: [:year,:month,:day])%>
<%= submit_tag %>
<%end%>
UPDATED with Final answer from chat
def index
start_date = params[:mileage_start]
end_date = params[:mileage_end]
if start_date && end_date
dates = get_start_and_end_date(start_date,end_date)
#mileages_pdf = Mileage.where(date: dates[:start_date]..dates[:end_date])
end
respond_to do |format|
# format.html # index.html.erb
# format.json { render json: #mileages }
format.pdf do
pdf = MileagePdf.new(#mileages_pdf, view_context)
send_data pdf.render, filename: "mileages_report.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
def get_start_and_end_date(start_date,end_date)
hsh = {}
hsh[:start_date] = build_date(start_date)
hsh[:end_date] = build_date(end_date)
hsh
end
def build_date(hsh)
DateTime.new(hsh["date(1i)"].to_i,hsh["date(2i)"].to_i,hsh["date(3i)"].to_i)
end
This way you can capture the date from params and format it in the way you want and place it in your query.

No route matches {:action=>"show", :controller=>"restaurants"}

If I want to go with my home page clicking on the map localhost:3000/maps gets out this error No route matches {:action=>"show", :controller=>"restaurants"}
controllers/maps_controller.rb
def index
#maps = Map.all
#json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow info_for_restaurant(map.restaurant)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #maps }
end
end
def show
#map = Map.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #map }
end
end
private
def info_for_restaurant(restaurant)
link_to restaurant_path do
content_tag("h2") do
restaurant.name
end
end
end
routes.rb
resources :restaurants
resources :maps
This is answer for my question:
controllers/maps_controller.rb
def index
#maps = Map.all
#json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow render_to_string(:partial => "/maps/maps_link",
:layout => false, :locals => { :map => map})
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #maps }
end
end
views/maps/_maps_link.html.erb
<div class="map-link">
<h2><%= link_to map.restaurant.title, map.restaurant %></h2>
</div>
You referred to restaurant_path within info_for_restaurant, which is part of MapsController. Rails met error here.
You need to either define the restaurant_path in restaurant controller, or comment out this function in maps controller at this moment.
Your approach is wrong in several levels. Let's work on them, one at a time:
1) Your call to the route helper is wrong:
restaurant_path is the route helper for a show action. A show action needs an id parameter to be valid. Your call is missing a parameter.
So, your code must be something like this:
def info_for_restaurant(restaurant)
link_to restaurant_path(restaurant) do
content_tag("h2") do
restaurant.name
end
end
end
To see the parameters needed for each action, you can run rake routes on the console.
However, this does not solve the problem, as you're also:
2) Calling view helpers from your controller
link_to and content_tag are view helper methods, and you don't want to bother your controller with view issues. So, the best way to solve this problem is to move your info_for_restaurant method to a helper, and call it from a view instead.
So, now, your controller will not assign anything to #json, and the last line of your view will look like this:
<%= gmaps4rails #maps.to_gmaps4rails {|map, marker| marker.infowindow info_for_restaurant(map.restaurant) } %>

Rails undefined method `map' for nil:NilClass

I'm trying to create a select field for a form that selects based on records selected for a model (called "Cancellation_Reasons").
In my model called Cancellation:
<%= form_for(#cancellation do |f| %>
<%= options_from_collection_for_select(#cancellation_reasons, :id, :name) %>
<% end %>
In the Cancellation_Controller:
def new
#cancellation = Cancellation.new
#cancellation_reasons = CancellationReason.find(1)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trade }
end
end
When I run CancellationReason.find(1) in the the Rails Console it finds the record, so #cancellation_reasons isn't nil. I think that it's probably in how I'm using the select helpers (I've tried experimenting with them, but I'm not quite sure which one to use even after reading the Rails Guide and Rails API docs).
options_from_collection_for_select expect a collection (even it it is a collection of 1).
So change the code to be:
def new
#cancellation = Cancellation.new
#cancellation_reasons = CancellationReason.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trade }
end
end

How to render JSON in Rails view

I've tryed the solution of following example: In Rails, how do you render JSON using a view?
But my problem is that the database already has a JSON string saved, I pull the json string and then I should be able to display the JSON file in the the view.
I'm using this because an android tablet should be able to visit the same url but depending on its settings (send by a POST) a different JSON file should be displayed, the android tablet then fetches the json and use it to configure some options.
So I already have a full working json file, i'm looking for a way to display it in a view (without rendering html tags and other stuff). I tryed the following (yes I've added respond_to :json) :
# def show_json (#config is the record, #config.json_config is the actual json configuration file
#config = event.et_relationships.where(terminal_id: terminal).first
respond_to do |format|
format.json
end
Then my view I have
#show_json.json.erb
<%= #config.config_json %>
Then the HTML I get to see (no errors are given)
<html><head><style type="text/css"></style></head><body></body></html>
Thanks!
EDIT
I'm using rails 3.2.3
Here is my routes (only relevant parts)
match '/events/config', to: "events#show_json", as: :show_json
resources :events do
member do
get :select_item
get :category
end
end
Then also the controller (partial)
respond_to :html, :json, :js
def show_json
#terminal_id = params["mac"]
terminal_id = "3D:3D:3D:3D:3D:3D"
event = current_user.events.where("date(endtime) > ? AND date(starttime) < ?", Time.now.to_s, Time.now.to_s).first
if event.nil?
# Nothing got returned so we use the default event
event = current_user.events.where('"default" = ?', true).first
end
logger.info "MAC: #{terminal_id}"
terminal = current_user.terminals.where(serial: terminal_id).first
logger.info "Terminal: #{terminal.attributes.inspect}"
logger.info "#{event.attributes.inspect}"
#config = event.et_relationships.where(terminal_id: terminal).first
logger.info "CONFIG #{#config[:config_json]}"
respond_to do |format|
format.json { render json: #config[:config_json] }
end
end
Use render:
#config = event.et_relationships.where(terminal_id: terminal).first
respond_to do |format|
format.json { render json: #config }
end
And then you have path /:model/:action.json.

Coffeebeans gem not working properly

So here's my controller:
class ScriptController < ApplicationController
respond_to :js
def show
puts 'here'
#client_id = params[:id]
respond_with #client_id
end
end
I have a file called in app/views/script/show.js.coffee but it doesn't load when I go to the show page. I'm using the coffeebeans gem: Loading .coffee files via a view in Rails
Any help?
EDIT: Added Error
Template is missing
Missing template good_comments/script/show, good_comments/application/show with {:handlers=>[:erb, :builder], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: * "/Users/shamoon/Sites/good_comments/spec/dummy/app/views" * "/Users/shamoon/Sites/good_comments/app/views"
Don't use CoffeeBeans, coffee-rails handles rendering of coffeescript out-of-box.
I render coffeescripts like this:
respond_to do |format|
format.html { redirect_to '/' }
format.js { render 'delete', :layout => false }
end