Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何重构我的Rails部分?_Ruby On Rails_Ruby - Fatal编程技术网

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

谢谢你,保罗。非常有用。