0

私のコントローラーには、次のように定義されたメソッドがあります。

def self.store_pdf(id)
...
end

そのメソッドでは、render_to_string を呼び出して正しいファイル/レイアウトをレンダリングする必要があります。

render_to_string(
    :action => "../view/current_version/show.pdf.erb", 
    :layout => false)

ただし、render_to_string はインスタンス メソッドであり、保護されているため、次のことを行う必要があります。

me = self.new # self is the cortroller
me.send(:render_to_string,
    :action => "../view/current_version/show.pdf.erb", 
    :layout => false)

ただし、次に示すように、render_to_string が機能する必要がある応答オブジェクトなどの依存関係があります: http://apidock.com/rails/ActionController/Base/render_to_string

それで、私はそれらを追加し始めました

me.send(:response=,  ActionController::Response.new)

しかし、ますます多くのグローバル インスタンス変数を定義する必要があり、1 つの静的メソッドを動作させるだけでは大変な作業になると判断しました。

このメソッドは静的である必要があります。これにより、delayed_job が後でメソッドをバックグラウンドで実行できるようになります。

誰でもこれをやってのける方法について考えがありますか?

4

1 に答える 1

1

Rails ヘルパーを使用していない場合は、ERB 経由で erb を読み取ることができます。Rails ヘルパーを使用している場合は、Rails ヘルパーを含めます。ここまたはを使用して参照できます

require 'erb'
class PdfRender
 #include ActionView::Helpers::OutputSafetyHelper
 #include helper if any is present any
 def self.render_pdf(id)
  #set any instance variable if you are using in pdf 
  content = File.read('path/of/erb/template')
  template = ERB.new(content) 
  # template content will give you text now you can render or generate pdf
  template_content = template.result(binding)


 end
end 

注: replace h()CGI.escapeHTML()

于 2012-09-06T12:33:50.440 に答える