Remote form Rails and select if the changes do not work.

I tried to follow: Rails: send (via AJAX) when clicking a dropdown

However, none of these options work (onsubmit, submit)

Here is what I am updating now on the page:

= simple_form_for task, :remote => true, :format => :js do |f|` = f.association :status, :label => false, :include_blank => false, :input_html => {:onchange=>'this.form.submit();' }` 

It just publishes the form regularly and makes the page refresh.

How can I get it to publish via ajax so that the page does not refresh?

+6
source share
3 answers

Although the rails override the default action and are sent via AJAX when you add: remote => true, you force the form to publish regularly what is in your onchange event.

You need to make your own function to send via AJAX and call it from your exchange using $ .post () $. post () to submit the form to the server.

+4
source

Just replace onchange with this:

 {:onchange=>'$(this.form).submit();' } 
+10
source

I ended up creating an event binding according to Marc's suggestion

 = simple_form_for task, :remote => true, :format => :js, :html => {:id => "task_#{task.id}_status"} do |f| = f.association :status, :label => false, :include_blank => false, :input_html => {:id => "task_#{task.id}_status_select" } :javascript jQuery(function($) { $(document).ready(function() { $("#task_#{task.id}_status_select").bind("change", function() { $.ajax({ type: "PUT", url: '#{project_story_type_story_task_path(@project, @story_type, @story, task)}', data: 'task[status_id]=' + $('#task_#{task.id}_status_select').val(), complete: function() { $('#task_#{task.id}_status').prepend("<span id='task_#{task.id}_success' class='label success right'>SAVED</span>"); $('#task_#{task.id}_success').delay(500).fadeOut(); } }); 
+4
source

Source: https://habr.com/ru/post/901783/


All Articles