user3283997
user3283997

Reputation:

Undefined method error in Rails 4

I'm trying to loop through a set of projects in the view using this code

<div class='forms-background'>
        <% @projects.each do |project| %>
        <div>
            <a href= "<%= user_path project.user %>">
            <img class= "" src= "<%=project.user.avatar_url %>" />
            <span class=""> <%= project.user.name %> </span>
            </a>
            @<%= project.user.username %>
            <span class= ""><%= time_ago_in_words(project.created_at) %></span>
            <p> <%= project.brief %></p>
        </div>
    <% end %>
    </div>

This is my controller method

def index
    @projects = Project.all
    @projects = Project.new
end

This is the error that I get.

undefined method `each' for #<Project:0x007fb8099f8b48>

Upvotes: 0

Views: 89

Answers (1)

cjn
cjn

Reputation: 1451

def index
    @projects = Project.all
    @projects = Project.new
end

I think you may have wanted @project = Project.new for the second Instance Variable. With both of them @projects, the second one will overwrite the first.

Upvotes: 2

Related Questions