ruby-on-railsbutton-to

button_to to render pdf format


In my controller I have the action:

def booking_sheet_report
  @groups = Group.all.order('priority DESC')
  respond_to do |format|
    format.html
    format.csv
    format.pdf
  end
end

and in the view I want the user to be able to open (download) the format.pdf when clicking on a button_to element (I do have a booking_sheet_report.pdf.erb view). With a link_to (<%= link_to 'PDF', booking_sheet_report_path(format: :pdf) %>) works fine.

I tried the followings without any success:

<%= button_to "PDF", {action: "booking_sheet_report",  :form => { "data-type" => "pdf" }}, {class: 'btn-u', method: :get} %>


<%= button_to "PDF", {action: "booking_sheet_report(format: :pdf)",  :form => { "data-type" => "pdf" }}, {class: 'btn-u', method: :get} %>

The second one gives an error: No route matches {:action=>"booking_sheet_report(format: :pdf)", :controller=>"reports", :form=>{"data-type"=>"pdf"}}

Any clue how is this possible or what I am doing wrong?

Edit: the one relevant route is get 'booking_sheet_report' => 'reports#booking_sheet_report'


Solution

  • No route matches {:action=>"booking_sheet_report(format: :pdf)", :controller=>"reports", :form=>{"data-type"=>"pdf"}}

    <%= button_to "PDF", {action: "booking_sheet_report(format: :pdf)",  :form => { "data-type" => "pdf" }}, {class: 'btn-u', method: :get} %>
    

    The problem with above code is you are closing the parenthesis(}) at the wrong side. I mean the closing parenthesis for :action => is placed on the wrong side. So it is considering the whole thing as url option and that is reported as a no route error. Also using parenthesis is always tricky.

    button_to(name = nil, options = nil, html_options = nil, &block) public

    The following should work

    <%= button_to "PDF", booking_sheet_report_path(format: :pdf), class: 'btn-u', method: :get, :form => {"data-type" => "pdf" } %>