Sushanth CS
Sushanth CS

Reputation: 2420

Rails: Change form action from submit_tag

I have a form which is always submitting the form to the "update" function in the controller, and I have created this form using "remote_form_for" tag. In this form I have objects from different tables, and from this form I want to submit entire form data to another function(not to the "update" function) via AJAX request.

I have tried many methods including using submit tag with action

<% remote_form_for @employee, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>
  // form objects and other functionalities
     ....
     ....  
         // views inside the forms
         <div id="employee_header_div">
           <%= render :partial => "employee_header", :locals => {:employee => @employee} %>
         </div>
         ...
         ... 
     <%= submit_tag "page_level_validation", :id => "page_level_validation" , :action=>"validate"%>
 <% end %>

But the Ajax request always calling the same "update" function.

It would be very helpful, if anyone helps to resolve this issue.

Upvotes: 4

Views: 4785

Answers (2)

Carpela
Carpela

Reputation: 2195

You can't set the submit to point to a different place than the main form has specified (unless you want to use the HTML5 formaction attribute and deal with the browser compatibility consequences).

However, what you could do is create a new action in your controller which deals with the situation.

e.g..

<% remote_form_for @employee, :url => organization_employee_validate_path(@organization, @employee), :method => :put do |employee_form| %>

in your controller

def validate
    #do something loosely based around the update method
end

not forgetting to add the appropriate routes.

Upvotes: 0

clemensp
clemensp

Reputation: 2525

Try this:

<% form_for @employee, :remote => true, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>

Upvotes: -1

Related Questions