0

我正在使用wicked_pdf gem 生成一个 pdf 文件:

只有当对象被持久化/保存在数据库中时,我才想生成 pdf :

像这样的东西:

def new
  @invoice = Invoice.new
  respond_to do |format|
    format.html # new.html.erb
  end
end

def create
  @invoice = Invoice.new(invoice_params)
  respond_to do |format|
    if @invoice.save
      format.html { redirect_to invoice_path(@invoice), notice: "Yesss!") }
    else
      format.html { render action: new, :alert => "Oops :(" }
    end
    format.pdf do
      @example_text = "exampletext"
        render :pdf => "file_name",
               :template => 'path/to/template'
    end
  end
end

但是这段代码只响应html。我想在里面生成pdf:

if @invoice.save
  #generate the pdf here
  format.html { redirect_to invoice_path(@invoice), notice: "Yesss!") }
else

我该怎么做,我可以使用回调吗?

4

1 回答 1

0

像这样删除respond_to块:

def create
...
  if @invoice.save
   render :pdf=>"file_name",
   :template=>'/path/to/template'
  else
    render action: "new", :alert=>"Oops :("
  end
 end
于 2013-05-21T20:46:07.683 回答