Ajax
我会认真考虑您的
Ajax刷新过程。刷新整个页面效率极低,我认为涵盖了更深层次的问题
为什么不尝试使用该
.js.erb文件
views夹中的文件来通过该方法直接处理JS:
def set_conflicts @conflict = @member.conflicts.for_date(params[:date]).first if @conflict @conflict.destroy else conflict_date = Date.parse(params[:date]) unless Conflict.create( month: conflict_date.month, day: conflict_date.day, year: conflict_date.year, member: @member ) flash[:alert] = 'Oops, there was a problem updating conflicts' end end flash[:notice] = 'This is a test!' respond_to do |format| format.html { redirect_to manage_member_conflicts_path(@member) } format.js endend#app/views/controller/set_conflicts.js.erb$("#your_element").html(<%=j render manage_member_conflicts_path(@member) ) %>);响应
尽管我不确定这一点,但我知道Flash对象是由ActionDispatch ::
Flash中间件处理的。我认为XHR不能像处理标准HTTP请求那样处理XHR,因此会阻止在响应中显示Flash
这意味着您必须做一些事情才能通过您的Ajax请求传递Flash对象。根据我在此答案顶部发布的问题,您可以使用
responseheaders来执行此操作:
class ApplicationController < ActionController::baseafter_filter :flash_to_headersdef flash_to_headers return unless request.xhr? response.headers['X-Message'] = flash[:error] unless flash[:error].blank? # repeat for other flash types... flash.discard # don't want the flash to appear when you reload pageend$(document).ajaxError(function(event, request) { var msg = request.getResponseHeader('X-Message'); if (msg) alert(msg);});


