在ajax调用之后重新加载Rails 4页面 [英] Rails 4 page reload after ajax call

查看:86
本文介绍了在ajax调用之后重新加载Rails 4页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that displays a large number of informational pages based on a location that I store in the session variable.

我在一个选项中使用了一个来自其他控制器的不同页面之间的部分共享。

I am using a single select in a partial shared between varied pages from other controllers.

_location_select.html.erb

_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

locations_controller.rb

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

这是我的coffescript:

Here is my coffescript:

home.js.coffee

home.js.coffee

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

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

All of this code works great, but I need to have the page reload after the ajax call is complete, It would be too complicated to have the controller return the updated html since the location select will appear on such a large number of pages.

我尝试添加成功:和错误:回调函数到ajax调用并且还使用了ajaxStop。我可以让ajax调用工作或页面重新加载工作,但无法找到获得两者的方法。

I have tried adding success:, and error: call back functions to the ajax call and have also played around with ajaxStop. I can get the ajax call to work or the page reload to work but cannot figure out the way to get both.

我尝试添加:

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

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

Where do I put location.reload() or is there a better way?

仅供参考,我在这个应用程序中没有使用turbolink。

FYI, I am not using turbolinks in this app.

推荐答案

locations_controller.rb

locations_controller.rb

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

views / locations / select.js

views/locations/select.js

window.location.reload();

这篇关于在ajax调用之后重新加载Rails 4页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆