0
votes

I'm doing the tutorial for RoR and am getting an exception:

SyntaxError in ArticlesController#index

"....rails/blog/app/views/articles/index.html.erb:18: syntax error, unexpected keyword_ensure, expecting end-of-input ensure ^"

It points to line 18 of an 16 line file. index.html.erb:

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

The articles_controller.rb file:

class ArticlesController < ApplicationController

  def index
    @articles = Article.all
  end

  def show
    @article = Article.find(params[:id])
  end

  def new
  end

  def create
    @article = Article.new(article_params)

    @article.save
    redirect_to @article
  end

  private
    def article_params
      params.require(:article).permit(:title, :text)
    end

end

Not sure where my error is. I'm using LightTable to edit the work.

2
@articles.each.do |article| should probably be @articles.each do |article|Brad Werth
Thank you! rusty debugging....JRaun

2 Answers

-1
votes

In Ruby do is a keyword that denotes a block. Not a method.

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

Additionally you are not properly checking if the article is valid in your create method:

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
-2
votes

You have a typo on your code, it should be <% @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>