Rails: change form action from submit_tag

I have a form that always submits the form of the "update" function in the controller, and I created this form using the "remote_form_for" tag. In this form, I have objects from different tables, and from this form I want to transfer all the form data to another function (and not to the "update" function) using an AJAX request.

I tried many methods, including using the submit tag with an 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 an Ajax request always calls the same "update" function.

It would be very helpful if anyone could help solve this problem.

+4
source share
2 answers

You cannot configure sending to a different place than the main form indicated (unless you want to use the HTML5 form5 attribute and deal with the effects of browser compatibility).

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

eg.

 <% 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.

0
source

Try the following:

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

All Articles