Ruby on rails 语法错误,意外的“|”
我一直在Rails4Ruby2应用程序中发现语法错误,但我不明白为什么。我想这是在10号线上发生的Ruby on rails 语法错误,意外的“|”,ruby-on-rails,ruby,Ruby On Rails,Ruby,我一直在Rails4Ruby2应用程序中发现语法错误,但我不明白为什么。我想这是在10号线上发生的 <% ('A'..'Z').each do |i| %> <h2><%= i.capitalize %></h2> <hr class="half-rule"> <% lesson_num = [] %> <% start_
<% ('A'..'Z').each do |i| %>
<h2><%= i.capitalize %></h2>
<hr class="half-rule">
<% lesson_num = [] %>
<% start_id = 0 %>
<% last_id = current_user.last_lesson %>
<% until start_id >= last_id do |x| %>
<% lesson_num << [x]%>
<% x += 1 %>
<% end %>
<% lesson_num.each do |id| %>
<% lesson = Lesson.find(id) %>
<% tags = @lesson.tags.split(',') %>
<% tags.each_with_index do |tag, index| %>
<% letter = tag.initial %>
<% if letter == i %>
<a href="/lesson/<%= id %>/step/1"><%= tag %></a>
<% else %>
<!-- else is only necessary if you actually need to put something here.-->
<% end %>
<% end %>
<% end %>
<% end %>
Ruby语句不向块传递参数,因此第8行的| x |无效
一个选项是将until替换为0..current_user.last_lession.each do | x |。您没有正确写入until循环;它不产生任何元素,因此| x |在这里不合适
此外,您可能想增加start_id,否则循环的条件值将永远不会计算为true
最后,您正在将数组[x]铲入数组lesson_num中。您可能不想这样做,因为稍后您将迭代lesson_num并将每个元素用作lesson表中一行的:id
尝试以下方法:
<% lesson_num = [] %>
<% start_id = 0 %>
<% last_id = current_user.last_lesson %>
<% until start_id >= last_id do %>
<% start_id += 1 %>
<% lesson_num << start_id %>
<% end %>
您还可以从中受益的是在模型之间建立关系,因为似乎每个用户都有许多课程,每个课程都属于一个用户。这将使您能够更轻松、更高效地访问用户课程并反复学习。i、 例如users.lessons.each do | lesson |。。。end在Ruby中,直到循环的语法是什么?那个循环中的什么改变了任何会改变循环条件真实性的东西?那个么简化是巨大的!非常感谢。它工作得很好,我可能会尝试建立你稍后推荐的协会。
<% (1..current_user.last_lesson).each do |id| %>
<% lesson = Lesson.find(id) %>