简体   繁体   中英

Re-rendering Rails partial AFTER saving the model through a form

I'm re-rendering a partial after submitting a remote: true form, but my problem is that it seems to re-render the partial BEFORE fully completing the form's update action. As a result, it renders old data on the page, and shows updated info only after a full refresh (or when I submit the form yet again). Is there a way to force the application to re-render the partial after everything is completed, in order to display only the new info? Or am I missing a step here?

update.js.erb:

$("#methods").html("<%= escape_javascript(render partial: 'methods') %>");

owner_controller.rb

respond_to :html, :js, :json

def update
  if owner.update_attributes(owner_params)
    respond_to do |format|
      format.js
    end
  else
    fail_update
  end
end

in update.js.erb

$("#methods").html("<%= j(render partial: 'methods') %>");
<% unless @owner.errors.present? %> 
 $('#some_form')[0].reset()
<% end %>
$("#methods").html("<%= escape_javascript(render partial: 'methods', locals: {owner: @owner}) %>");

假设您在之前的操作中设置了所有者,请将您设置的变量从所有者更改为@owner。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM