【问题标题】:Rails flash with AJAX request without page refreshRails 使用 AJAX 请求闪烁,无需页面刷新
【发布时间】:2017-03-06 06:21:58
【问题描述】:

我希望有一种清洁方法可以让 Flash 消息在使用 React 的 ajax 调用中工作。一直是looking on the web,老实说,我确信无需添加太多代码就可以完成。

def my_method
 [...]

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

我没有进行页面重定向,我想知道如何让一个简单的 Flash 显示出来。围绕显示添加更多方法的答案。我可以在我的方法中包含闪存代码吗?比如:

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

【问题讨论】:

    标签: ruby-on-rails ajax reactjs


    【解决方案1】:

    Flash 在页面重新加载时起作用,因此在出现 Ajax 请求时,您可以将消息作为响应发回并使用 JS 显示。或者,您可以渲染一个js.erb 模板并在其中使用JS 显示消息。

    希望有帮助!

    【讨论】:

    • 所以这是唯一的方法?
    • 我猜是的,这是唯一的办法!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多