Rails 3 / 控制器 / 闪存哈希

发布于 2024-10-15 07:20:18 字数 355 浏览 5 评论 0原文

我希望能够将多条消息传递到控制器内部的闪存哈希,并让它们很好地显示在一起,例如在项目符号列表中。我设计的方法是在我的应用程序控制器中创建一个辅助函数,它将数组格式化为项目符号列表,然后将其传递给(在我的例子中)flash[:success]。这显然不是 Rails 方式,因为我的项目符号列表被编码了。也就是说,

  • 是: &
  • lt

我得到的

;ul>

  • Message 1
  • Message 2
  • 我确信我可以找到一种方法来 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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    幸福不弃 2024-10-22 07:20:18

    我使用 render_to_string 和部分而不是助手来实现类似的效果。

    # app/controller/dogs_controller.rb
    def create
      @dog = Dog.new(params[:dog])
      @messages=[]
      if @dog.save
        @messages << "one"
        @messages << "two"
        flash[:notice] = render_to_string( :partial => "bulleted_flash")
        redirect_to(dogs_path)
      else
        render :action => 'new
      end
    end
    

    然后,我将 Flash 消息数组格式化为 HTML 列表

    # app/views/dogs/_bulleted_flash.html.erb
    <ol>
      <% @messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ol>
    

    ,该列表会生成以下 HTML

    # http://0.0.0.0:3000/dogs
    <body>
      <div id="flash_notice">
        <ul>
          <li>one</li>
          <li>two</li>
        </ul>
      </div>
      ...
    </body>
    

    如果您需要继续使用帮助程序,那么我认为您需要将 html_safe 方法附加到您的字符串中,以防止它被编码(rails 3 默认情况下)。这是一个 问题,显示如何以类似的方式使用 html_safe

    I used render_to_string and a partial instead of a helper to achieve something similar.

    # app/controller/dogs_controller.rb
    def create
      @dog = Dog.new(params[:dog])
      @messages=[]
      if @dog.save
        @messages << "one"
        @messages << "two"
        flash[:notice] = render_to_string( :partial => "bulleted_flash")
        redirect_to(dogs_path)
      else
        render :action => 'new
      end
    end
    

    Then I format the array of flash messages in an HTML list

    # app/views/dogs/_bulleted_flash.html.erb
    <ol>
      <% @messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ol>
    

    Which produces the following HTML

    # http://0.0.0.0:3000/dogs
    <body>
      <div id="flash_notice">
        <ul>
          <li>one</li>
          <li>two</li>
        </ul>
      </div>
      ...
    </body>
    

    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 use html_safe in a similar fashion

    东走西顾 2024-10-22 07:20:18

    如果您使用 Rails3,请尝试原始方法。

    raw(my_html_string)
    

    而且它不会转义 html。哦,抱歉,我刚刚读到你的最后一句话。查看此信息“Rails 3 flash message issues”,它看起来可能就是您正在寻找的内容:

    http://www.ruby-forum.com/topic/215108

    If you are using Rails3, try the raw method.

    raw(my_html_string)
    

    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

    怪我太投入 2024-10-22 07:20:18

    通常,在这种情况下,我会要求您提供有关视图和布局的更多信息,因为脚手架默认情况下不显示 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] %>

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文