ksugiarto
ksugiarto

Reputation: 951

Form Different Submit Action

I have a form to showing a report, but I need 3 features which is print to pdf, print to excel, and also showing preview via html itself using js

This is the first condition that only for pdf and excel and work well for both.

controller:

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

view:

<%= form_tag(models_path, :method => "get") do %>
  <%= submit_tag "#{t 'pdf'}", :name => "print" %>
  <%= submit_tag "#{t 'excel'}", :name => "excel" %>
<% end %>

But when I start adding preview feature through js, it not error, but both submit button (pdf n excel) pointing to js.

new controller

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

new view

<%= 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 is pointing on application.js

$.fn.ajaxFilter = function() {
  this.submit(function() {
    $.get(this.action, $(this).serialize(), null, "script");
  return false;
  });
}

$("#headers_search").ajaxFilter();

So, I confuse on where I did mistake.. I'm kind of rails newbie. Thanks

Upvotes: 0

Views: 507

Answers (2)

Kami
Kami

Reputation: 19437

The jquery seralize() method does not serialise submit buttons. See the docs at Jquery - Serialize. This means that when you submit the form via jquery, none of the submit buttons are included.

You need to edit the jquery submission to include which button was clicked for your code to work.

See the answer to the question at - jQuery: how to get which button was clicked upon form submission? on how to determine which button was pressed.

Upvotes: 0

Zippie
Zippie

Reputation: 6088

Try with

if params[:name] == "print"

Upvotes: 1

Related Questions