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| %>
If you're referencing something in your main application layout then it must be loaded for every controller. Putting it in one specific controller only engages it when that controller is in use, which it will only be on very specific routes.
If @projects
needs to be populated on every request, move that to a load_projects
method and add a before_action
filter in your main ApplicationController. Putting it in a default index
method doesn't work, that will never get run.
You can always stub in Rails.logger.debug('...')
type calls to see if your code is actually being exercised. Watch log/development.log
constantly to see what's happening. Most problems can be quickly resolved by examining what's been logged.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.