Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 如何在助手方法中循环,并为每个循环实例渲染分部。轨道_Ruby On Rails_Partials - Fatal编程技术网

Ruby on rails 如何在助手方法中循环,并为每个循环实例渲染分部。轨道

Ruby on rails 如何在助手方法中循环,并为每个循环实例渲染分部。轨道,ruby-on-rails,partials,Ruby On Rails,Partials,我在Rails应用程序中有以下视图: app/views/matches/_round.html.erb 同样,出于同样的原因,作为字符串的第一个条件输出也应放入results数组中: # more code here if random_predictions_for_match.empty? results << ["<h3> No predictions to spy available yet </h3>".html_safe] else # mo

我在Rails应用程序中有以下视图:

app/views/matches/_round.html.erb

同样,出于同样的原因,作为字符串的第一个条件输出也应放入
results
数组中:

# more code here
if random_predictions_for_match.empty?
  results << ["<h3> No predictions to spy available yet </h3>".html_safe]
else
# more code here
#这里有更多代码
如果随机预测匹配为空?

结果首先是完全正切:你知道你不必打开/关闭
Taryn吗非常感谢你的建议。这绝对有道理。出于某种原因,我不愿意在数组中收集渲染部分。不过,我已经尝试了您建议的解决方案,当语句或块未正确关闭时,我会遇到类似的语法错误。我已经检查过了,问题肯定出在渲染部分被推到数组中的行上。
results hmmm。。。也许它希望它是一个字符串而不是。。。例如
results=“”
results+=…
我会从简单的格式开始测试它(例如一个带有预测id的字符串,而不是一个部分模板),并检查所有其他逻辑是否正常工作。。。然后添加不带局部变量的渲染。。。看看它在什么地方断裂谢谢Taryn。我现在正在再试一次。一定很简单。这让我感到困惑,因为在某个时候它起了作用,我看不出它是什么,失去了改变,天知道是怎么回事。我会按照你的调试建议去做,看看我是否幸运,我有错误。我认为Ruby中的空格并不重要。在这种情况下不行!我在
render
和第一个括号之间添加了一个空格。辅助对象现在返回一个数组,其中每个渲染部分都包含一个数组。但是,浏览器没有将其作为HTML读取。我尝试在部分帮助程序的末尾使用
html\u-safe
,希望得到一个简单的修复,但没有成功。我可以通过调用另一个助手而不是渲染部分来解决整个问题,因此最终我可以不带逻辑地离开视图。然而,如果你能建议最后一个Rails魔术来解决这个问题,那将是值得赞赏的。重新思考这个问题,实际上问题是,在你提出的方法中,无论我是在数组中收集部分还是其他助手输出,匹配
每个
循环中的助手输出都将始终是一个数组。这意味着我必须在我的主视图中循环它,并且当helper方法没有输出数组时(就像第一个条件的情况一样),如果没有放入另一个数组,它将破坏代码。我将尝试这个方法并报告它是否有效。
  def predictions(pool, match, participant)
    if match.can_be_predicted_in?(pool)
      random_predictions_for_match = match.random_predictions_in_pool(pool)
      if random_predictions_for_match.empty?
        "<h3> No predictions available yet </h3>".html_safe
      else
        random_predictions_for_match.each do |prediction|
          tooltip_title = "undisclosed participant"
          outcome_class = ""
          render partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class }
        end
      end
    else
      match.predictions_in_pool(pool, current_user).each do |prediction|
        tooltip_title = prediction.participant.username
        outcome_class = prediction.class_name
        render partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class }
      end
    end
  end
matches.each do |match|
<% if match.can_be_predicted_in?(@pool) %>
  <% random_predictions_for_match = match.random_predictions_in_pool(@pool) %>
  <% if random_predictions_for_match.empty? %>
    <h3> No predictions to spy available yet </h3>
  <% else %>
    <% random_predictions_for_match.each do |prediction| %>
      <% tooltip_title = "undisclosed participant" %>
      <% outcome_class = "" %>
      <%= render partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class } %>
    <% end %>
  <% end %>
<% else %>
  #etc, etc, the rest of the code below.
end
matches.each do |match|
<% predictions(@pool, match, @current_participant).each do |prediction| %>
  <%= prediction %>
<% end %>
end
# more code here
if random_predictions_for_match.empty?
  results << ["<h3> No predictions to spy available yet </h3>".html_safe]
else
# more code here
<% matches.each do |match|
 if match.can_be_predicted_in?(@pool)
   random_predictions_for_match = match.random_predictions_in_pool(@pool) 
   if random_predictions_for_match.empty? %>
    <h3> No predictions to spy available yet </h3>
  <% else 
     random_predictions_for_match.each do |prediction| 
       tooltip_title = "undisclosed participant" 
       outcome_class = "" %>
      <%= render partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class } %>
    <% end 
   end 
 else %>
  # etc, etc, the rest of the code below.
<% end %>
  def predictions(pool, match, participant)
    results = []
    if match.can_be_predicted_in?(pool)
      random_predictions_for_match = match.random_predictions_in_pool(pool)
      if random_predictions_for_match.empty?
        return "<h3> No predictions available yet </h3>".html_safe
      else
        random_predictions_for_match.each do |prediction|
          tooltip_title = "undisclosed participant"
          outcome_class = ""
          results << render(partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class })
        end
      end
    else
      match.predictions_in_pool(pool, current_user).each do |prediction|
        tooltip_title = prediction.participant.username
        outcome_class = prediction.class_name
        results << render(partial: 'match_predictions/prediction', locals: { match: match, prediction: prediction, tooltip_title: tooltip_title, outcome_class: outcome_class })
      end
    end
    results
  end