Rails 3 / 控制器 / 闪存哈希
我希望能够将多条消息传递到控制器内部的闪存哈希,并让它们很好地显示在一起,例如在项目符号列表中。我设计的方法是在我的应用程序控制器中创建一个辅助函数,它将数组格式化为项目符号列表,然后将其传递给(在我的例子中)flash[:success]。这显然不是 Rails 方式,因为我的项目符号列表被编码了。也就是说,
- 是: &
- lt
我得到的
;ul>
我确信我可以找到一种方法来 raw() 输出,但是没有一种简单的方法可以让这样的东西工作吗?也许有一个选项可以传递给 flash[]?还有别的事吗?
I want to be able to pass multiple messages to the flash hash, inside of my controller, and have them display nicely together, e.g., in a bulleted list. The way I've devised to do this is to create a helper function in my Application Controller, which formats an array into a bulleted list, which I then pass to, in my case, flash[:success]. This is clearly not the Rails Way because, i.a., my bulleted list gets encoded. That is, instead of getting:
- Message 1
- Message 2
I get:
<ul><li>Message 1</li><li>Message 2</li></ul>
I'm sure I could figure out a way to raw() the output, but isn't there a simple way to get something like this working? Perhaps there's an option to pass to flash[]? Something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用 render_to_string 和部分而不是助手来实现类似的效果。
然后,我将 Flash 消息数组格式化为 HTML 列表
,该列表会生成以下 HTML
如果您需要继续使用帮助程序,那么我认为您需要将
html_safe
方法附加到您的字符串中,以防止它被编码(rails 3 默认情况下)。这是一个 问题,显示如何以类似的方式使用html_safe
I used
render_to_string
and a partial instead of a helper to achieve something similar.Then I format the array of flash messages in an HTML list
Which produces the following HTML
If you need to continue using a helper then I think you need to append the
html_safe
method to your string to prevent it from being encoded (which rails 3 does by default). Here is a question showing how to usehtml_safe
in a similar fashion如果您使用 Rails3,请尝试原始方法。
而且它不会转义 html。哦,抱歉,我刚刚读到你的最后一句话。查看此信息“Rails 3 flash message issues”,它看起来可能就是您正在寻找的内容:
http://www.ruby-forum.com/topic/215108
If you are using Rails3, try the raw method.
And it won't escape the html. Oh, sorry, I just read your last sentence. Check out this information, "Rails 3 flash message problems", it looks like it may be what you are looking for:
http://www.ruby-forum.com/topic/215108
通常,在这种情况下,我会要求您提供有关视图和布局的更多信息,因为脚手架默认情况下不显示
flash[:success]
。我解决这个问题的方法是通常完全重做我的 flash 消息,每次将 flash[:whatever] 制作为一个数组,并在我的布局中处理该数组而不仅仅是通知。请记住,闪存只是一个哈希,您只是设置值。
但是,如果您只想使用现在的设置执行此操作(帮助程序将 HTML 放入
flash[:success]
中),您可以更改 Flash 消息在布局文件中的显示方式。默认情况下,它们只是使用<%= flash[:success] %>
,它会自动转义 HTML。要使其不对 Flash 消息执行此操作,请将其更改为<%=raw flash[:success] %>
Usually I would ask for more information about your views and layouts in this situation, because scaffolding doesn't display
flash[:success]
by default.The way I solve this is to totally redo my flash messages usually, by making the
flash[:whatever]
an array every time, and in my layout handling that array instead of just the notice. Remember that flash is just a Hash, you're just setting values.However if you just want to do this with the setup you have now (helper putting the HTML inside the
flash[:success]
), you can change the way that the flash messages are displayed in your layout file. By default they are just use<%= flash[:success] %>
, which automatically escapes HTML. To make it not do that for the flash messages, change it to<%=raw flash[:success] %>