Ruby on rails 如何重构我的Rails部分?
我有一个带有较深条件的分部,我很好奇这是否适合分部 我在一系列游戏中循环,并根据游戏日期是否与今天的日期匹配来打印是或否Ruby on rails 如何重构我的Rails部分?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个带有较深条件的分部,我很好奇这是否适合分部 我在一系列游戏中循环,并根据游戏日期是否与今天的日期匹配来打印是或否 <% i = 0 %> <% x = 0 %> <% @games.each do |game| %> <% if game.date.strftime("%_m/%d")[1..-1] == today %> YES! <% i = 1 %> <% else %>
<% i = 0 %>
<% x = 0 %>
<% @games.each do |game| %>
<% if game.date.strftime("%_m/%d")[1..-1] == today %>
YES!
<% i = 1 %>
<% else %>
<% unless i == 1 %>
NO!
<% i = 1 %>
<% x = 1 %>
<%= next_game %>
<% end %>
<% end %>
<% end %>
对
不
谢谢 这似乎对你来说很好,但我不确定为什么你有两个变量,或者为什么你有
next\u game
<% @games.each do |game| %>
<% if game.date == Date.today %>
YES
<% else %>
NO!
<% end %>
<% end %>
对
不
我会把全部投入某种帮助方法:
app/views/your\u controller/your\u view/your\u partial.html.erb
<% @games.each do |game| %>
<%= played_today(game) %>
<% end %>
def played_today(game) # or whatever you want to call it
# Use Date.current to be timezone aware, otherwise just Date.today
game.date == Date.current ? 'YES' : 'NO'
end
<% = render 'your_partial', games: @games %>
<% games.each do |game| %>
<%= played_today(game) %>
<% end %>
我不确定你今天的和下一场游戏的变量/方法具体做了什么,或者你是否真的想保留那些I
/x
局部变量,不管出于什么原因,但如果你这样做了或者它们有其他额外的意义,请详细说明你的问题
另外,我建议:
app/views/your_controller/your_view.html.erb
<% @games.each do |game| %>
<%= played_today(game) %>
<% end %>
def played_today(game) # or whatever you want to call it
# Use Date.current to be timezone aware, otherwise just Date.today
game.date == Date.current ? 'YES' : 'NO'
end
<% = render 'your_partial', games: @games %>
<% games.each do |game| %>
<%= played_today(game) %>
<% end %>
app/views/your\u controller/\u your\u partial.html.erb
<% @games.each do |game| %>
<%= played_today(game) %>
<% end %>
def played_today(game) # or whatever you want to call it
# Use Date.current to be timezone aware, otherwise just Date.today
game.date == Date.current ? 'YES' : 'NO'
end
<% = render 'your_partial', games: @games %>
<% games.each do |game| %>
<%= played_today(game) %>
<% end %>
谢谢你,保罗。非常有用。