Rails 4 页面在 ajax 调用后重新加载

Rails 4 page reload after ajax call

本文关键字:新加载 加载 调用 ajax Rails      更新时间:2023-09-26

我有一个应用程序,它根据存储在会话变量中的位置显示大量信息页面。

我正在从其他控制器的不同页面之间部分共享中使用单个选择。

_location_select.html.erb

<%= select_tag(:location, options_for_select(Location.all.order('name ASC').collect { |l| [l.name, l.id] }, [session[:location]||'']), include_blank: true) %>

这会对以下位置进行 ajax 调用:

locations_controller.rb

class LocationsController < ApplicationController
  def select
    session[:location] = params[:value]
    render js: ''
  end
end

这是我的咖啡稿:

首页.js.咖啡

$ ->
  $('#location').change ->
    $.get '/location/select', {value: $('option:selected', this).val()}

所有这些代码都很好用,但我需要在 ajax 调用完成后重新加载页面,让控制器返回更新的 html 太复杂了,因为位置选择将出现在如此大量的页面上。

我尝试将成功:和错误:回调函数添加到 ajax 调用中,并且还尝试了 ajaxStop。我可以让 ajax 调用工作或页面重新加载工作,但无法弄清楚两者兼而有之的方法。

我尝试添加:

$(document).ajaxStop(function(){
    window.location.reload();
});

我在哪里放置 location.reload() 还是有更好的方法?

仅供参考,我没有在此应用程序中使用涡轮链接。

locations_controller.rb

class LocationsController < ApplicationController
  def select
    session[:location] = params[:value]
    respond_to do |format|
        format.js   {}
    end
  end
end

视图/位置/选择.js

window.location.reload();