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 に答える