开发者

Tidyest way to process generated view before returning

I have this in my controller. I need my view to generate some HTML, which I then convert to a pdf using Flying Saucer (Im using JRuby). This then gets returned to the client.

  def show    
    @cs_agreement = CsAgreement.find(params[:id])

    respond_to do |format|
      format.pdf do
        # TODO there must be a better way of getting the path to the view!
        report_template = ERB.new(File.new("app/views/agreement_document/client_agreement.erb"), nil, "%<")
        created_report = report_template.result(bin开发者_如何学JAVAding)

        send_data( FlyingSaucer::create_pdf(created_report), :filename => "agreement.pdf",
                   :type => "application/pdf",
                   :disposition => 'inline')
      end
    end

This is the best I can do... I can only work out how to use ERB to manually generate the html so I can retreive the data to send to FlyingSaucer before returning.

This way also seems to mean I cant access any of the Helper methods.

Is there no way I can get rails to generate the html for me and somehow intercept it to convert to pdf before returning? The render function seems to just generate and return all in one..


You can use render_to_string, as in

report_template = render_to_string 'client_agreement'

Same options as render. More details here.

If you need to specify a different controller / action, you can do that too, eg via :action => :action_name.


Use render_to_string.

def show    
  @cs_agreement = CsAgreement.find(params[:id])

  respond_to do |format|
    format.pdf do
      result = render_to_string
      send_data( FlyingSaucer::create_pdf(result), :filename => "agreement.pdf",
               :type => "application/pdf",
               :disposition => 'inline')
    end
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜