简体   繁体   English

如何访问 jQuery 中的 rails 2 flash[] hash?

[英]How do you access the rails 2 flash[] hash in jQuery?

Ok, I'm sure there is something simple that I'm missing here, but I can't quite seem to find the answer.好的,我确定我在这里缺少一些简单的东西,但我似乎无法找到答案。

I have a modal that I updates both via normal HTTP request and now I'm adding some unobtrusive AJAX with jQuery to process the same request.我有一个模态,我通过正常的 HTTP 请求更新,现在我添加一些不显眼的 AJAX 和 jQuery 来处理相同的请求。

After the controller action I have user messages delivered in the flash[] hash (IE flash[:errors], flash[:message]) This is then displayed in on the redirect to page.在 controller 操作之后,我在 flash[] hash (IE flash[:errors], flash[:message]) 中传递了用户消息,然后显示在重定向到页面上。

My question is: How do I access this flash hash in my respond to.js file?我的问题是:如何在我的 response.js 文件中访问这个 flash hash? What I want to do is something like:我想做的是:

$("#message").append(ADD FLASH MESSAGE HERE || render a message partial)

Your jquery code can be treated just like any other (html) part of the partial template.您的 jquery 代码可以像部分模板的任何其他 (html) 部分一样处理。 So for your example, you should be able to use:因此,对于您的示例,您应该能够使用:

$("#message").append('<%= flash[:error] || render(:partial => :my_msg_partial) %>')

You could try something like flash.to_json您可以尝试类似flash.to_json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM