What I Would like to achieve is that Document that is generated in PDF should be based on search engine. I used ransack as the tool in order to do searching criteria. I can generate the document in PDF with code below but it generates the whole records although search engine is working perfectly fine. How can I Generate the PDF based on specific criteria? Thanks a lot
This is My Controller
def index
@search = Student.search(params[:q])
@students = @search.result.paginate(:page => params[:page], :per_page => 5)
@search.build_condition if @search.conditions.empty?
@search.build_sort if @search.sorts.empty?
@total_students = Student.count
@numMale = Student.where(:gender => "Male").count
@numFemale = Student.where(:gender => "Female").count
respond_to do |format|
format.html
format.json { render json: @students }
format.pdf {@search.result}
end
end
And this is apps/views/students/index.pdf.prawn
header = ['First Name', 'Last name', 'Date Of Birth', 'Gender', 'District','Sub distrcit','Village','Hamlet']
@search = Student.search(params[:q])
@students = @search.result.paginate(:page => params[:page], :per_page => 5)
students = @students.search.result.map do |student|
[
student.fname,
student.familyName,
student.dob,
student.gender,
student.district.name,
student.subdistrict.name,
student.suco.name,
student.village.name
]
end
pdf.table [header] + students, :header => true, :width => pdf.bounds.width do
row(-5..-1).column(2).align = :right
row(0).style :font_style => :bold
row(-1).style :font_style => :bold
end
This is my apps/views/students/index.html.erb
<%- model_class = Student -%>
<div class="page-header">
<h1>List of Learners</h1>
</div>
<fieldset class="scheduler-border">
<legend class="scheduler-border">Total Students</legend>
<div class="container">
<p>Number of Students:<%= @total_students%></p>
<p>Number of Males:<%= @numMale%></p>
<p>Number of Females:<%= @numFemale%></p>
</div>
</fieldset>
<fieldset class="scheduler-border">
<legend class="scheduler-border">Search Students</legend>
<div id="advancesearch" style='display:none'>
<fieldset class="scheduler-border">
<legend class="scheduler-border">Advance Search</legend>
<%= search_form_for @search, url: search_students_path, method: :post do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c %>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
<div class="col-sm-4">
Sort:
<%= f.sort_fields do |s| %>
<%= s.sort_select Hash.new, class: "form-control" %>
<% end %>
</div>
<br>
<div class="actions"><%= f.submit "Filter", class: "btn btn-primary" %></div>
<% end %>
</fieldset>
</div>
<%= search_form_for @search, url: search_students_path, method: :post do |f| %>
<div class="col-sm-4">
<%= f.text_field :fname_cont, class: "form-control", :placeholder =>"Type first name" %>
</div>
<div class="col-sm-4">
<%= f.text_field :familyName_cont, class: "form-control" , :placeholder =>"Type Family name"%>
</div>
<div class="col-sm-4">
<%= f.submit "Search", class: "btn btn-primary" %>
</div>
<% end %>
<div class="col-sm-4">
<br>
<input type="button" name="answer" class="btn btn-primary" value="Advance Search" onclick="showDiv()" />
</div>
</div>
<br>
</fieldset>
<div class="pagination">
<%= will_paginate @students, renderer: BootstrapPagination::Rails %>
</div>
<div class="pdf_link">
<%= link_to "Generate PDF", students_path(@students, :format => "pdf"), class: "btn btn-primary" %>
</div>
<br>
<table class="table table-striped">
<thead>
<tr>
<th><%= sort_link @search, :id, "Student Number" %></th>
<th><%= sort_link @search, :fname, "First name" %></th>
<th><%= sort_link @search, :tname, "Middle Name" %></th>
<th><%= sort_link @search, :familyName, "Surname" %></th>
<th><%= sort_link @search, :gender, "Gender" %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @students.each do |student| %>
<tr>
<td><%= link_to student.student_number, student_path(student) %></td>
<td><%= student.fname %></td>
<td><%= student.tname %></td>
<td><%= student.familyName %></td>
<td><%= student.gender %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_student_path(student), :class => 'btn btn-default btn-xs' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
student_path(student),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-xs btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_student_path,
:class => 'btn btn-primary' %>
I have Just Look around and I find this useful answer in this link :
Ransack export results to CSV.
Just add The following code.
<div class="pdf_link">
<%= link_to "Generate PDF", students_path(params.merge(:format => "pdf")), class: "btn btn-primary" %>
</div>