2

wicked-pdf と imgkit をそれぞれ使用して、ユーザーが PDF または JPG 形式でドキュメントを印刷できるアプリを作成しています。PDF用とJPG用の2つのボタンがあります。これらのボタンが、ここでは「作成」であるコントローラーの同じアクションを指すようにすることは可能ですか? 私のボタンは-

<%= button_to "Print Bill[PDF]", :action => "create" %>

<%= button_to "Print Bill[JPG]", :action => "new" %>

両方のアクションを作成できますか? はいの場合、どのように?ヒットしたボタンをキャッチして、それぞれのビューをレンダリングする方法。

4

1 に答える 1

1

First of all, it is generally recommended to use route helpers, rather than specify controllers and actions. So your code could be

<%= button_to "Print Bill[PDF]", bill_print_path(@bill, format: :pdf) %>
<%= button_to "Print Bill[JPG]", bill_print_path(@bill, format: :jpeg) %>

and in your controller

def print
    # insert here code to find your bill and load it from DB
    respond_to |format| do
        format.jpeg do
            # code to produce the jpeg version of the bill
        end
        format.pdf do
            # code to produce the pdf version of the bill
        end
    end
end

As a final step I would change button_to to link_to and style your link as a button, but that is more of a personal preference.

于 2015-04-12T12:11:35.187 に答える