【问题标题】:View logic with a block and conditional rails使用块和条件导轨查看逻辑
【发布时间】:2012-10-02 04:14:09
【问题描述】:

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

我想迭代创建的物流,如果物流标题(通过“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>

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

感谢您的关注和关注。

【问题讨论】:

    标签: ruby-on-rails-3 view controller logic


    【解决方案1】:

    您说得对,您认为有很多重复的代码和逻辑。如果我要自己执行此操作,我可能会更改控制器,以便您的 @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>
    

    【讨论】:

    • 非常感谢。这正是我一直在寻找的答案,是像你这样的人让我坚持了这么久。 == 是它的工作原理,我想你可以说 v1,我希望应用程序做的事情。现在,从您回答的第一部分开始,我可以研究将代码变成“最佳实践版本”,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多