简体   繁体   中英

show record on wicked_pdf on ruby on rails 5?

So i wanted show on pdf file just selected record. Now i get all record, or just first one when i search for localhost:3000/service_orders.pdf . When click the button "print" when they just give error something link this.

ServiceOrdersController#show is missing a template for this request format and variant.

My file.pdf.erb :

<%  @service_order ||= ServiceOrder.find(params[:id]) do |service_order| %>
    <td> <%= service_order.number %> </td> ...
    %< end %>

My service orders controller :

     def index
        @service_orders = ServiceOrder::Collector.call(params: params.permit!.to_h)
        respond_to do |format|
          format.html
          format.json
          format.pdf {render :pdf => "service_orders/file",
          :template => '/file', formats: :html, encoding: 'utf8'}
        end
      end
    ...
    ...
   def show
     @service_order = ServiceOrder.find(params[:id])
   end

My link button is on show.html.erb

<%= link_to 'Print', service_order_path(@service_order, :format => :pdf),:format => :pdf,  class:"button-show "%>

And of course my mimi_types.rb file include Mime::Type.register "application/pdf", :pdf

I been looking for internet, and closest thing was this. I hope someone was the same problem.

You do not specify format in show action so by default it tries to render html template. You have to add:

   def show
     @service_order = ServiceOrder.find(params[:id])
     respond_to do |format|
          format.html
          format.json
          format.pdf do 
            render pdf: "name_of_template",
            some other options
          end
        end
   end

And locate template for this action in views/service_orders/show.pdf.erb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM