Rails在没有页面刷新的情况下使用AJAX请求闪烁

时间:2016-10-24 12:54:14

标签: ruby-on-rails ajax reactjs

我希望有一种清理方法可以使用React在ajax调用中使用flash消息。已经looking on the web了,老实说,我确信无需添加太多代码即可完成。

def my_method
 [...]

 flash[:success] = "Yea bouy."
 if request.xhr?
    render :json => {
                        :order => true,
                    }
 end
end

我没有进行页面重定向,并且想知道如何让简单的flash显示出来。 Anwers周围显示添加更多方法。我可以在我的方法中使用flash代码吗?类似的东西:

def my_method
 [...]
 if request.xhr?
    render :json => {
                        :order => true,
                    }
 end
 return flash[:success] = "Yea bouy." # Only works when page refreshes.
end

1 个答案:

答案 0 :(得分:0)

Flash在页面重新加载时工作,因此在Ajax请求的情况下,您可以将消息作为响应发送回来并使用JS显示它。或者,您可以使用JS来呈现js.erb模板并在其中显示消息。

希望有所帮助!