Reload Partial (Rails Ajax)

妖精的绣舞 提交于 2019-11-29 21:34:33

问题


I have a form that when I click submit, should update a partial that is on the same page... When I place this here:

page.replace_html 'show_cards_div', 'Hello'

It does just as it should, display hello when I need it to. But when I make it into this....

page.replace_html 'show_cards_div', :partial => "reloadThisPartial"`

It simply does not do anything. What am I doing wrong?


回答1:


The request might not be an Ajax call. Make Ajax call. User remote_form_for instead of form_for or use jquery.form for sending ajax calls to controller. If you are using jquery then do something like:

<%= javascript_include_tag "jquery-1.4.4.min.js", "jquery.form" %>
<%= form_for :user, :url => {:controller =>  "come_controller",:action => "some_action"}, :html => {:id => 'form_id', :class => 'cmxform', :onsubmit => "return false;"} do |f| %>
......................................
<% end %>

<script type="text/javascript">
$('#form_id').submit(function() {
var container = $("#show_cards_div");
            $(this).unbind('submit').ajaxSubmit({
              success: function(data) {
                     container.html(data);
              }
            })
            return false
          });
</script>

in controller do something like this.

render :partial => "reloadThisPartial", :layout => false, :locals =>{:users =>@users}


来源:https://stackoverflow.com/questions/6114220/reload-partial-rails-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!