Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails submit form w/o refresh, :remote => true

I'm trying to submit a form in rails without refreshing the page afterwards. I've been looking around online, but it seems that adding :remote => true doesn't seem to change my form the way I thought it would. Right now I have each question having a number of answers, and each one is connected to a radio button, but I've hidden the radio button so clicking on the label itself submits the form. Does the page refresh as a result of the radio button form submission? I'm really not sure, and would appreciate any help at this point...

<%= form_for(uanswer, :remote => true) do |f| %>
    <% answers.each_with_index do |answer, i| %>
    <% unless answered_flag %>
        <%= f.radio_button :answer_id, answer.id, :class => "radio hide", 
            :onclick => "this.form.submit();" %>
    <% end %>
    <%= f.label :answer_id, answer.answer, :class => "answer",
        :value => answer.id %>
<% end %>

The generated HTML form looks more or less like this:

<form accept-charset="UTF-8" action="/uanswers" class="new_uanswer" 
 data-remote="true" id="new_uanswer" method="post"><div  
 style="margin:0;padding:0;display:inline"><input 
 name="utf8" type="hidden" value="✓"><input name="authenticity_token"
 type="hidden" value="PY5ACVmrvDnt/iYF8RK6O7tDKAn2G2dFdLeBNZw5MJ4="></div>
</form>
like image 498
vivianh Avatar asked Nov 23 '25 03:11

vivianh


1 Answers

The remote=>true isn't doing anything for you right now as you are bypassing rails with the this.form.submit(). You need to submit the form asynchronously. See Submit form in rails 3 in an ajax way (with jQuery) for a solution to a similar question.

like image 172
Brian Chapman Avatar answered Nov 25 '25 17:11

Brian Chapman