我有一个表单来显示报告,但我需要3个功能print to pdf
,print to excel
,并且还使用js
这是仅适用于pdf
和excel
的第一个条件,对两者都有效。
控制器:
respond_to do |format|
if params[:print]
format.html { redirect_to :action => "report", :format => "pdf", :start_period => params[:start_period], :end_period => params[:end_period], :warehouse => params[:warehouse] }
elsif params[:excel]
format.html { redirect_to :action => "report", :format => "xls", :start_period => params[:start_period], :end_period => params[:end_period], :warehouse => params[:warehouse] }
else
format.html
end
end
查看:
<%= form_tag(models_path, :method => "get") do %>
<%= submit_tag "#{t 'pdf'}", :name => "print" %>
<%= submit_tag "#{t 'excel'}", :name => "excel" %>
<% end %>
但是当我开始通过js
添加预览功能时,它不是错误,而是提交按钮(pdf n excel)指向js 。
新控制器
respond_to do |format|
if params[:print]
format.html { redirect_to :action => "report", :format => "pdf", :start_period => params[:start_period], :end_period => params[:end_period], :warehouse => params[:warehouse] }
elsif params[:excel]
format.html { redirect_to :action => "report", :format => "xls", :start_period => params[:start_period], :end_period => params[:end_period], :warehouse => params[:warehouse] }
else
format.html
format.js # NEW LINE
end
end
新视图
<%= form_tag(models_path, :method => "get", :id => "headers_search") do %>
<%= submit_tag "#{t 'ep.submit'}", :name => nil, :remote => true %>
<%= submit_tag "#{t 'pdf'}", :name => "print" %>
<%= submit_tag "#{t 'excel'}", :name => "excel" %>
<%= end %>
headers_search
指向 application.js
$.fn.ajaxFilter = function() {
this.submit(function() {
$.get(this.action, $(this).serialize(), null, "script");
return false;
});
}
$("#headers_search").ajaxFilter();
所以,我对我错误的地方感到困惑。我是铁杆新手。感谢
答案 0 :(得分:1)
尝试
if params[:name] == "print"
答案 1 :(得分:0)
jquery seralize()
方法不会序列化提交按钮。请参阅Jquery - Serialize处的文档。这意味着当您通过jquery提交表单时,不会包含任何提交按钮。
您需要编辑jquery提交以包含单击了哪个按钮才能使代码生效。
请参阅问题的答案 - jQuery: how to get which button was clicked upon form submission?,了解如何确定按下了哪个按钮。