Rails 产量 - content_for 问题

发布于 2024-09-16 20:07:05 字数 1122 浏览 9 评论 0原文

我有以下要求。

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

示例页面结构类似于

所有交易的总和 - 200

交易金额 特恩1 100 特恩2 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 %>

并且

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

我正在使用 Rails 2.2.2

请帮助我让我知道是否有更好的方法,

提前感谢欢呼

Sameera

编辑

实际上我想做的是,在特定循环之前显示一些详细信息,可以在循环之后收集这些详细信息

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

谢谢

I have the following requirement.

Ex: There is a transaction table where it has columns say, transaction_name and amount. I want to loop through the transactions and display their details (transaction_name and amount) and finally I want to display the total amount (sum of all the amounts) in the head (before the loop) section of my page. (Think about it as a summary display)

Example page structure would be like

Sum of all the transactions - 200

transaction amount
trn1 100
trn2 50
trn3 50

And I tried to use yield and content_for tag but no luck.

my code is as follows (i'm calling inside my erb file.)

<%= 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 %>

And

I'm using with inside a view (not inside a layout)

I'm using rails 2.2.2

Please help me and let me know if there is a better way

thanks in advance

cheers

sameera

EDIT:

Actually what I want to do is , Display some details before a particular loop where those details can be collected after the loop

Ex: If i have an array of transaction objects, I want to show a count of pass and failed transactions before the transactions loop in my view

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

筱武穆 2024-09-23 20:07:05

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

在 Rails 4 中,您可以 传递 :同花=> true 作为 content_for 的选项:

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

在 Rails 3.1.1(大约)中,您可以 通过定义和使用以下内容(例如在 application_helper 中),在屈服时从 view_flow 中删除内容

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

In other cases where you really need to reuse content_for, the following may be useful.

In Rails 4, you can pass :flush => true as an option to content_for:

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

In Rails 3.1.1 (approx) you can delete the content from the view_flow when you yield by defining and using the following (eg in application_helper):

def yield_and_flush!(content_key)
  view_flow.content.delete(content_key)
end
柠檬色的秋千 2024-09-23 20:07:05

我认为您对 content_for 和产量有错误的想法。 :) 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 的评论。整个干燥原则并不真正适用于执行微小的逻辑,例如循环数组。

I think you have the wrong idea about content_for and 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>

edit -

Regarding collecting data, I suggest you put them in helper methods:

#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

Just noticed your comment to theTRON. The whole dry principle doesn't really apply to executing tiny logic such as looping through a array.

没︽人懂的悲伤 2024-09-23 20:07:05

我会编写一个单独计算总数的辅助方法,也许类似于:

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

然后您可以将其显示在视图中的任意位置,方法是:

<%= calculate_total(@transactions) %>

I would write a helper method which calculates the total separately, perhaps something along the lines of:

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

Then you can display it in your view wherever you like, with:

<%= calculate_total(@transactions) %>
天气好吗我好吗 2024-09-23 20:07:05

在 Rails 3 中,您可以在 content_for; 之后产生yield所以你的代码将变成:

<% 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 %>

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

In Rails 3 you can yield after content_for; so your code will become:

<% 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 %>

Note:

<% content_for :transaction_summary do %>

doesn't have to be inside of

<% content_for :transaction_table do %>

, but for some more complex cases it could.

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