Reputation: 1065
So I've searched this and most people have gotten this error because they have their private method in the middle of their Class or they have records in the db that have nil attributes. As it stands, I only have one record in the db, which I will show below. I get this error when trying to access an index page I'm creating.
Controller:
class AnthologiesController < ApplicationController
before_action :authenticate_user!, :except => [:show, :index]
def index
@anthologies = Anthology.all
end
def show
@anthology = Anthology.find(params[:id])
end
def new
@anthology = Anthology.new
end
def create
@anthology = Anthology.new(anthology_params)
@anthology.user = current_user
if @anthology.save
redirect_to @anthology, notice: "Success! Your anthology was created."
else
render 'new'
end
end
private
def anthology_params
params.require(:anthology).permit(:title, :description)
end
end
View:
<h1>Anthologies</h1>
<ul>
<%= @anthologies.each do |anthology| %>
<li><%= @anthology.title %></li>
<li><%= @anthology.description %></li>
<li><%= @anthology.username %></li>
<% end %>
</ul>
And the one record in the db:
#<Anthology id: 1, title: "Writing More Better", description: "This is me teaching you writing and suchwhat.", created_at: "2015-11-26 15:57:40", updated_at: "2015-11-26 15:57:40", user_id: 1>
Any help would be amazing, and thanks in advance.
Upvotes: 1
Views: 618
Reputation: 1601
Even you can also try this:
<h1>Anthologies</h1>
<ul>
<%= @anthologies.each do |anthology| %>
<li><%= anthology.try(:title) %></li>
<li><%= anthology.try(:description) %></li>
<li><%= anthology.try(:username) %></li>
<% end %>
</ul>
Upvotes: 0
Reputation: 2844
Inside the each
loop you are referring to @anthology
instead of the block variable you are declaring.
<h1>Anthologies</h1>
<ul>
<%= @anthologies.each do |anthology| %>
<li><%= anthology.title %></li>
<li><%= anthology.description %></li>
<li><%= anthology.username %></li>
<% end %>
</ul>
Upvotes: 3