【问题标题】:Rails yield - content_for problemRails 产量 - content_for 问题
【发布时间】:2011-04-06 18:45:35
【问题描述】:

我有以下要求。

例如:有一个事务表,其中包含事务名称和金额列。我想遍历交易并显示它们的详细信息(transaction_name 和金额),最后我想在页面的头部(循环之前)部分显示总金额(所有金额的总和)。 (将其视为摘要显示)

示例页面结构如下

所有交易的总和 - 200

交易金额 trn1 100 trn2 50 trn3 50

我尝试使用 yield 和 content_for 标签,但没有成功。

我的代码如下(我在我的 erb 文件中调用。)

<%= yield :transaction_summary %> 

<table>
  <% total_amount = 0%>
  <%for transaction in @transactions%>
    <tr>
      <td><%= transaction.transaction_name %></td>
      <td><%= transaction.amount %></td>
      <% total_amount += transaction.amount %>
    </tr>
  <%end%>
  </table>

<% content_for :transaction_summary do %>
   <h1>
     Sum of all the transactions - <%= total_amount %>
   </h1>
<% end %>

还有

我在视图内使用 with(不在布局内

我正在使用 rails 2.2.2

请帮助我,如果有更好的方法,请告诉我

提前致谢

干杯

同人

编辑:

其实我想做的是,在特定循环之前显示一些细节,这些细节可以在循环之后收集

例如:如果我有一组交易对象,我想在我的视图中的交易循环之前显示通过和失败交易的计数

谢谢

【问题讨论】:

    标签: ruby-on-rails views yield content-for


    【解决方案1】:

    在 Rails 3 中,您可以在 content_for 之后屈服;所以你的代码会变成:

    <% content_for :transaction_table do %>
      <table>
        <% total_amount = 0%>
        <% for transaction in @transactions do %>
          <tr>
            <td><%= transaction.transaction_name %></td>
            <td><%= transaction.amount %></td>
            <% total_amount += transaction.amount %>
          </tr>
        <%end%>
      </table>
    
      <% content_for :transaction_summary do %>
         <h1>
           Sum of all the transactions - <%= total_amount %>
         </h1>
      <% end %>
    <% end %> <!- End content_for :transaction_table -->
    
    
    <%= yield :transaction_summary %> 
    <%= yield :transaction_table %>
    

    注意:

    <% content_for :transaction_summary do %>
    

    不必在内部

    <% content_for :transaction_table do %>
    

    ,但对于一些更复杂的情况,它可以。

    【讨论】:

      【解决方案2】:

      在您确实需要重用 content_for 的其他情况下,以下内容可能会有用。

      在 Rails 4 中,您可以将 pass :flush =&gt; true 作为 content_for 的选项:

      <% content_for :example, flush: true do %>
        <h1>Deletes previous content</h1>
      <% end %>
      

      在 Rails 3.1.1(大约)中,您可以通过定义和使用以下内容(例如在 application_helper 中)delete the content from the view_flow when you yield

      def yield_and_flush!(content_key)
        view_flow.content.delete(content_key)
      end
      

      【讨论】:

      • :flush =&gt; true 让我很开心。非常感谢。
      【解决方案3】:

      我认为您对 content_for 和 yield 有错误的想法。 :) http://guides.rubyonrails.org/layouts_and_rendering.html

         <h1>
           <%= @transactions.collect(&:amount).sum -%>
         </h1> 
         <table>
            <%for transaction in @transactions%>
              <tr>
                <td><%= transaction.transaction_name %></td>
                <td><%= transaction.amount %></td>
              </tr>
            <%end%>
          </table>
      

      编辑-

      关于收集数据,我建议你把它们放在辅助方法中:

      #transactions_helper.rb
      def transactions_total transactions
        @transactions_total ||= @transactions.collect(&:amount).sum
      end
      
      def passed_transactions transactions
        @passed_transactions ||= @transactions.collect{|transaction| transaction.passed == true}
      end
      
      def failed_transactions transactions
        @failed_transactions ||= transactions - passed_transactions(transactions)
      end
      

      刚刚注意到您对 theTRON 的评论。整个干原理并不真正适用于执行微小的逻辑,例如遍历数组。

      【讨论】:

      • 嗨,马克,感谢您的重播。尽管您的示例有效,但我还有更多功能,例如获取通过/失败的事务计数等。我在想的是,因为我可以在循环后获取所有详细信息,是否有任何 Rails 方式将这些详细信息注入html DIV 在循环上方我已经更新了我的帖子,提到了我想做的事情你是对的,我想我想念理解 content_for 标签的概念,再次感谢,欢迎任何帮助:D
      • 嗨,马克感谢更新“整个干原理并不真正适用于执行微小的逻辑,例如遍历数组”,这是一个很好的观点。欢呼sameera
      【解决方案4】:

      我会编写一个辅助方法来单独计算总数,可能类似于:

      # app/helpers/transactions_helper.rb
      def calculate_total(transactions)
       total = 0
       transactions.each {|transaction| total += transaction.amount}
       total
      end
      

      然后你可以在任何你喜欢的地方显示它,使用:

      <%= calculate_total(@transactions) %>
      

      【讨论】:

      • 嗨 theTRON 感谢您的回复。但我想要一种在一个循环中获取这些细节的方法。由于您提出的方法在同一个结果集中包括两个循环,即 1 - 辅助方法 2 - 在视图中显示详细信息,想知道是否有更多 DRY 方法可以做到这一点,欢呼sameera
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2012-02-29
      • 2011-12-02
      • 2022-08-22
      • 1970-01-01
      • 2013-08-30
      相关资源
      最近更新 更多