whkpdf在rails中会出现ActionController::UnknownFormat错误

3

我将尝试将wicked_pdf集成到我的rails 4应用程序中。但是我遇到了“UnknownFormat”错误。

在gem文件中,我已经添加了:

  gem 'wicked_pdf'

在我的学生控制器中,我定义了以下show方法:

   controllers/students_controllers

   respond_to :html, :js, :pdf

   def show
     @student = Student.find(params[:id])
     respond_to do |format|
       format.pdf do
         render :pdf => "students",:template => "students/show_pdf"
       end
   end

我有一个名为show_pdf.pdf.erb的模板,位于我的学生视图文件夹中。不过,我已经在代码中添加了Wicked_pdf引擎路径。

initializers/wicked_paf.rb 

WickedPdf.config = {
   :exe_path => '/usr/bin/wkhtmltopdf'
}

我还在initilizers/mime_type.rb中添加了pdf的mime类型。

  initializers/mime_type.rb

  Mime::Type.register "application/pdf", :pdf

但是运气不好,我收到了未知格式错误。
  ActionController::UnknownFormat at /students/1
  ActionController::UnknownFormat

我不知道这个错误的原因。请帮我找出问题所在。
提前感谢。

1
我猜是因为你没有访问 /students/1.pdf。 - j-dexx
这应该解决http://stackoverflow.com/questions/22877724/wicked-pdf-too-few-arguments-in-demo/22878206#22878206问题。 - Nithin
3个回答

6
  1. You don't have to add "pdf" as a new mime_type in Rails 4 (it's already there).

  2. There is a typo in your file_path: "initializers/wicked_paf.rb" instead of "initializers/wicked_pdf.rb".

  3. Try to change the #show in controller this way:

    def show
        respond_to do |format|
          format.html
          format.pdf do
            render :pdf => "students"
          end
        end
    end
    
当然,您需要在'app/views/students/show.html.erb'中为其创建适当的视图。

0

请确保在您的link_to中包含format: 'pdf'

link_to 'My PDF', print_pdf_path, format: 'pdf'

0
您需要将 .pdf 添加到您的端点上: 例如:api/reports/1.pdf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接