Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails remote form and select on change not working

I was trying to follow: Rails: submit (via AJAX) when drop-down option clicked

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

This is what I currently have that is refreshing 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();' }`

This simply posts the form regularly and causes the page to refresh.

How can I get it to post via ajax so the page won't refresh?

like image 555
Josh Avatar asked Nov 18 '11 04:11

Josh


2 Answers

Just change your onchange to this instead:

{:onchange=>'$(this.form).submit();' }
like image 98
cilphex Avatar answered Nov 18 '22 02:11

cilphex


Although rails overrides the default action and submits via AJAX when you add :remote => true, you are forcing the form to post the regular what in your onchange event.

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

like image 5
Marc Avatar answered Nov 18 '22 02:11

Marc