I've been searching for hours but I can't find the answer anywhere. I'm new to ruby on rails and I can't figure out how to fix this. What caused the problem is that I moved an instance variable from one file to another and now no links are working and the error always displays: undefined method `each' for nil:NilClass
here is my code:
Application.html.erb:
<% number = 1 %>
<% @projects.each do |project| %>
<%= link_to project, id: "a-#{number}" do %>
<div class="flex">
<div class="each--project" id="project-<%= number %>">
<h3><%= project.title %></h3>
<p class="date"><%= project.created_at.strftime("%A, %b %d")%></p>
<p class="content"><%= project.description %></p>
</div>
</div>
<% number = number + 1 %>
<% end %>
<% end %>
application_controller.rb
class ApplicationController < ActionController::Base
def index
@projects = Project.all
end
protect_from_forgery with: :exception
end
projects_controller
class ProjectsController < ApplicationController
before_action :find_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@projects = Project.all.order("created_at desc")
end
def new
@project = Project.new
end
def create
@project = Project.new project_params
if @project.save
redirect_to @project, notice: "Yay Mia! That project was saved!"
else
render 'new'
end
end
def show
end
def edit
end
def update
if @project.update project_params
redirect_to @project, notice: "Yay Mia! That project was updated!"
else
render 'edit'
end
end
def destroy
@project.destroy
redirect_to projects_path
end
private
def find_project
@project = Project.friendly.find(params[:id])
end
def project_params
params.require(:project).permit(:title, :description, :link, :slug)
end
end
routes rb
Rails.application.routes.draw do
devise_for :users
resources :posts
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
get '*path' => redirect('/')
end
You don't have an index
action on the ApplicationController
You can however achieve the same thing with a before_action
if you want it loaded for all actions in all controllers. This is not something I would recommend though.
class ApplicationController < ActionController::Base
before_action :load_projects
def load_projects
@projects = Project.all
end
end
Hint:
<% number = 1 %>
<% @projects.each do |project| %>
can be much better written as
<% @projects.each_with_index do |project, number| %>