2

在此处输入图像描述我想要一些经验丰富的资深人士关于如何找到答案的建议。我知道我认为我的逻辑太多了,我在重复自己,但我仍在尝试“边做边学”,这就是我要让它发挥作用的地方,我会学习如何当然要从那里重构。

我想迭代创建的物流,如果logistic_title(通过“X”、“Y”或“Z”的形式传递)与“X”匹配,则在 div 中显示 X,否则不显示任何内容。

<div class="container-fluid">
<div class="row-fluid">
<div class="span4 hero-unit">
   <% @logistics.each do |logistic| %>
      <% if logistic.logistic_title = "Practice" %><br>
       <%= logistic.logistic_title %>
       <%= logistic.user.full_name %>
       <%= logistic.content %><br>
       <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
       <%= link_to "Edit", edit_logistic_path(logistic) %> |
       <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
     <% else %>
      <%= puts "" %>  
     <% end %> 
   <% end %>
</div>
<div class="span4 hero-unit">
  <% @logistics.each do |logistic| %>
     <% if logistic.logistic_title = "Game" %><br>
      <%= logistic.logistic_title %>
      <%= logistic.user.full_name %>
      <%= logistic.content %><br>
      <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
      <%= link_to "Edit", edit_logistic_path(logistic) %> |
      <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %>
     <%= puts "" %>  
    <% end %> 
  <% end %>
</div>
<div class="span4 hero-unit">
  <% @logistics.each do |logistic| %>
     <% if logistic.logistic_title = "Etc." %><br>
      <%= logistic.logistic_title %>
      <%= logistic.user.full_name %>
      <%= logistic.content %><br>
      <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
      <%= link_to "Edit", edit_logistic_path(logistic) %> |
      <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %>
     <%= puts "" %>  
    <% end %> 
  <% end %>
</div>

我知道我需要写一些帮助的东西,但正确的方向会有所帮助。请温柔,这就是我的学习方式。

感谢您的关注和关注。

4

1 回答 1

3

你是对的,你的视图中有很多重复的代码和逻辑。如果我要自己执行此操作,我可能会更改控制器,以便您的 @logistics 实例变量根据标题拆分为三个实例变量:

@practice = @game = @etc = []
@logistics.each do |logistic|
  if logistic.title == 'Practice'
    @practice << logistic
  elsif logistic.title == 'Game'
    @game << logistic
  elsif logistic.title == 'Etc.'
    @etc << logistic
  end
end

关于助手,我认为如果您正在编写标准 Rails 应用程序并且您的控制器名称与模型匹配,则通常会为您的助手创建 link_to 和logistic_edit_path 助手,因此您不需要自己添加这些助手。

当您指的是相等运算符时,请注意不要使用单个等号 (=)。我认为您的代码可能应该如下所示:

<div class="container-fluid">
<div class="row-fluid">
<div class="span4 hero-unit">
   <% @logistics.each do |logistic| %>
      <% if logistic.logistic_title == "Practice" %>
       <br>
       <%= logistic.logistic_title %>
       <%= logistic.user.full_name %>
       <%= logistic.content %><br>
       <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
       <%= link_to "Edit", edit_logistic_path(logistic) %> |
       <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
     <% else %>
      <%= puts "" %>  
     <% end %> 
   <% end %>
</div>
<div class="span4 hero-unit">
  <% @logistics.each do |logistic| %>
     <% if logistic.logistic_title == "Game" %><br>
      <%= logistic.logistic_title %>
      <%= logistic.user.full_name %>
      <%= logistic.content %><br>
      <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
      <%= link_to "Edit", edit_logistic_path(logistic) %> |
      <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %>
     <%= puts "" %>  
    <% end %> 
  <% end %>
</div>
<div class="span4 hero-unit">
  <% @logistics.each do |logistic| %>
     <% if logistic.logistic_title == "Etc." %><br>
      <%= logistic.logistic_title %>
      <%= logistic.user.full_name %>
      <%= logistic.content %><br>
      <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> |
      <%= link_to "Edit", edit_logistic_path(logistic) %> |
      <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %>
     <%= puts "" %>  
    <% end %> 
  <% end %>
</div>
于 2012-10-02T04:32:25.287 回答