我想要一些经验丰富的老手建议,告诉我如何找到答案。我知道在我看来,我有太多的逻辑,我正在重复自己,但我仍然在努力“从实践中学习”,这就是我要在哪里开始工作,我会学习如何当然要从那里重构。
我想迭代创建的物流,如果logistics_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>
我知道我需要写一些帮手,但正确方向上的一点有帮助。 请温柔,这就是我的学习方式。
感谢您的关注和关注。
答案 0 :(得分: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>