Javascript 如何在rails 5中使用format.pdf响应
我正在尝试使用jquery获取显示pdf页面的控制器操作。在javascript方式之前,rails方式是有效的: Show.html.erbJavascript 如何在rails 5中使用format.pdf响应,javascript,ruby-on-rails,ruby,ajax,wicked-pdf,Javascript,Ruby On Rails,Ruby,Ajax,Wicked Pdf,我正在尝试使用jquery获取显示pdf页面的控制器操作。在javascript方式之前,rails方式是有效的: Show.html.erb <%= link_to "Get PDF", client_path(@client.id, format: :pdf) %> 控制器: $.ajax({ url: "/clients/" + clientId + ".pdf", type: 'POST', // Ive also tried GET and created a p
<%= link_to "Get PDF", client_path(@client.id, format: :pdf) %>
控制器:
$.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
<p id="foo"></p>
$("#foo").text("Bar");
如果我点击我的rails按钮,我会得到pdf视图,但对于带有按钮点击功能的普通按钮,情况并非如此。如何使用ajax实现这一点?我有成功日志,但仅此而已
编辑:
$.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
<p id="foo"></p>
$("#foo").text("Bar");
如何将条码
发送到我的控制器?我需要在我的pdf中有Bar
show.html.erb:
$.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
<p id="foo"></p>
$("#foo").text("Bar");
所以基本上我需要传入几个参数:
<%= link_to "Get PDF", client_path(@client.id, format: :pdf) %>
如果需要发送参数,可以通过在路线代码中添加参数来完成
如果需要使用ajax进行链接,则添加
remote:true
也在其中。你需要传递什么参数来点击按钮?我有一些用javascript填充的文本,比如:
和js:$(“#foo”).text(“Bar”)
所以我还需要将条
传递给控制器,这样我认为ajax最好?您是否尝试将remote添加到link_to
,应该可以。例如: