1

I draw bar graphs in my app using gruff. In one view, 'compare', I want to show one graph for every 'step' my 'project' has. I imagine it something like this:

image_tag graph_step_path(step)

I've created a 'graph' action in the step controller and included a route. But now I'm kind of lost. How can my controller action return the correct image path to the image_tag function? Doing 'return' doesn't feel right. Or could I somehow directly include the image blob generated by gruff?

edit:

I've created a helper function that creates the graph and returns the file path.

4

1 に答える 1

5

コントローラー メソッドで、パスではなく実際の画像を返したいとします。リクエストごとにイメージをディスクに書き込むと、非常に遅くなります。これを行うには、Rails の send_data メソッドを使用します。

send_data(g.to_blob, :filename => "any.png", :type => 'image/png', :disposition=> 'inline')

g はあなたの無愛想なインスタンスです。ファイル名はまったく関係ありません。イメージ タグの src は、グラフを生成する特定のコントローラー メソッドである必要があります。これを生成するには、通常の URL ヘルパーを使用できます。

于 2011-04-29T14:02:24.010 に答える