Rails入门部分5.8 - ArticlesController #index中的SyntaxError

时间:2017-05-09 23:52:56

标签: ruby-on-rails

我正在做the tutorial for RoR并且遇到异常:

  

ArticlesController #index

中的SyntaxError      

" .... rails / blog / app / views / articles / index.html.erb:18:语法错误,   意外的keyword_ensure,期待输入结束确保^"

它指向16行文件的第18行。 index.html.erb:

Select-Object BaseName

articles_controller.rb文件:

<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  <% @articles.each.do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
    </tr>
  <% end %>
</table>

不确定我的错误在哪里。我使用LightTable编辑作品。

2 个答案:

答案 0 :(得分:-1)

在Ruby do中是一个表示块的关键字。不是一种方法。

<% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.text %></td>
    <td><%= link_to 'Show', article_path(article) %></td>
  </tr>
<% end %>

此外,您未正确检查文章在create方法中是否有效:

def create
  @article = Article.new(article_params)
  if @article.save
    redirect_to @article
  else
    render :new # renders /app/views/articles/new.html.erb
  end
end

答案 1 :(得分:-2)

您的代码上有拼写错误,应为<% @articles.each do |article| %>

<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
    </tr>
  <% end %>
</table>