Ask A Question


You’re not receiving notifications from this thread.

How to generate PDF based on search using prawn in ruby on rails

Francisco Quinones asked in Gems / Libraries

So Chris im using prawn pdf in my app.
Now im adding some search form to filter by date range. All works fine and I get my filter table on my index. my problem is with prawn that wont hold the filter records

my form filter range

        <%= form_tag({controller: "accidents", action: "index"}, {:class => 'navbar-form navbar-left', method: "get"}) do %>
              <%= text_field_tag :from_date, params[:from_date], class: "form-control", placeholder: "Fecha Inicial" %>
              <%= text_field_tag :to_date, params[:to_date], class: "form-control", placeholder: "Fecha Final" %>
              <%= submit_tag 'Buscar', class: "btn btn-primary" %>
        <% end %>

my controller code

  def index
    if params[:from_date] && params[:to_date].present?
       from_date = Date.strptime(params[:from_date], "%d/%m/%Y")
       to_date = Date.strptime(params[:to_date], "%d/%m/%Y") +
       @accidents = @project.accidents.sorting.where(:date => from_date..to_date)
       .paginate(:page => params[:accidents], :per_page => 10)
        @accidents = @project.accidents.sorting.paginate(:page => params[:accidents], :per_page => 10)
    respond_to do |format|
        # format.csv { send_data  @accidents.to_csv}
        format.pdf do
          pdf =,@project)
          send_data pdf.render,filename: "accidente_reporte_#{}.pdf",
                              type: "application/pdf",
                              disposition: "inline"

This my index filter with a range of date

when I go to see the pdf version

<%= link_to Report, project_incidents_path(format: "pdf"), :class=> "btn" %>

I get

maybe I need to past the dates to the link_to pdf. If so how is the right way to pass that data.


I found that this works

<%= link_to Report, project_incidents_path(params.merge(format: "pdf")), :class=> "btn" %>

I get this

but I dont know if is the best way.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.