最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - rails submit form wo refresh, :remote => true - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question asked Apr 26, 2013 at 17:33 vivianhvivianh 1551 gold badge3 silver badges12 bronze badges 1
  • Whatever gets rendered after the form is submitted depends on what the action that the form is posted to decides to render. As you haven't actually posted that controller action in your question I can't provide a proper answer other than to suggest you try render :nothing => true – jamesc Commented Apr 26, 2013 at 18:00
Add a ment  | 

2 Answers 2

Reset to default 9

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.

Changing this.form.submit() to $(this).closest('form').submit() worked for me. I use the "onchange" method instead on "onclick" for my form fields though.

发布评论

评论列表(0)

  1. 暂无评论