0
我試圖使用jquery獲取顯示pdf頁面的控制器操作。 JavaScript的方式之前,軌方式工作:如何使用format.pdf在rails中響應5
Show.html.erb
<%= link_to "Get PDF", client_path(@client.id, format: :pdf) %>
的問題是,我需要一些PARAMS,我從JavaScript得到這麼一個解決辦法是使用Ajax發出請求:
JS:
$.ajax({
url: "/clients/" + clientId + ".pdf",
type: 'POST', // Ive also tried GET and created a post route in routes.rb
data: { //...
},
success: function(data, xhr) {
//...
console.log('Success.....');
},
error: function() {
console.log("Error....")
}
});
控制器:
respond_to :html, :xml, :json
def show
respond_with do |format|
format.pdf do
render pdf: "demopdf",
template: "clients/show.pdf.html.erb",
locals: {:client => @client}
end
end
end
如果我點擊我的軌道按鈕,我得到的PDF查看但不能因此與點擊一個按鈕功能的普通按鈕。如何用ajax做到這一點?我得到了成功日誌,但就是這樣。
編輯:
如何獲得Bar
我的控制器?我需要在我的pdf中有Bar
?
show.html.erb:
<p id="foo"></p>
JS:
$("#foo").text("Bar");
所以基本上我需要通過一些PARAMS在:
<%= link_to "Get PDF", client_path(@client.id, format: :pdf) %>
你需要傳遞什麼參數來點擊按鈕? – sahil
我有幾個文本是用javascript填充的,ie:'
'和js:'$(「#foo」)。text(「Bar」);'所以我需要將'Bar'傳遞給控制器我認爲阿賈克斯會是最好的? – Sylar您是否嘗試將'remote_to'添加到遠程,應該可以工作。例如:<%= link_to「Get PDF」,client_path(@ client.id,format::pdf,remote:true)%>' – sahil