使用Wicked PDF gem / Rails 3项目的未初始化的常量Mime :: PDF

时间:2012-05-05 10:00:22

标签: ruby-on-rails ruby-on-rails-3 wicked-pdf

当我尝试提交表单时,我收到此错误(PDF应该使用Wicked PDF gem生成,在表单提交时) -

NameError in PostsController#create

uninitialized constant Mime::PDF
Rails.root: /Users/fkhalid2008/littlechits

Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:42:in `create'
app/controllers/posts_controller.rb:39:in `create'

我该如何解决这个问题?相关代码如下。

POSTS CONTROLLER

 def create
    @post = Post.new(params[:post])
    @post.user = current_user

    respond_to do |format|
        if verify_recaptcha && @post.save
            format.html { redirect_to :action=> "index"}
            format.pdf do
                render :pdf => "file_name"
                end
        else
            format.html { render :action => "new" }
            format.json { render :json => @post.errors, :status => :unprocessable_entity }
        end
    end
end

CONFIG /初始化/ WICKED_PDF.RB

# config/initializers/wicked_pdf.rb
WickedPdf.config = {
:exe_path => '/usr/local/bin/wkhtmltopdf'
}

谢谢,

费萨尔

1 个答案:

答案 0 :(得分:11)

您需要在config / initializers / mime_types.rb中定义pdf MIME类型

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