简体   繁体   English

我正在为nil:NilClass获取一个未定义的方法“ each”…但是它已定义?

[英]I'm getting an undefined method `each' for nil:NilClass…but it is defined?

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 引起问题的原因是我将实例变量从一个文件移动到了另一个文件,现在没有链接可用,并且错误始终显示:nil:NilClass的未定义方法“ each”

here is my code: 这是我的代码:

Application.html.erb: 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 application_controller.rb

    class ApplicationController < ActionController::Base
      def index
         @projects = Project.all
      end
      protect_from_forgery with: :exception
    end

projects_controller 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 路线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. 您在ApplicationController上没有index动作。但是,如果希望为所有控制器中的所有动作加载它,则可以使用before_action实现相同的操作。 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. 如果需要在每个请求中填充@projects ,请将其移至load_projects方法,然后在主ApplicationController中添加一个before_action过滤器。 Putting it in a default index method doesn't work, that will never get run. 将其放在默认index方法中不起作用,它将永远不会运行。

You can always stub in Rails.logger.debug('...') type calls to see if your code is actually being exercised. 您始终可以在Rails.logger.debug('...')类型调用中进行存根,以查看您的代码是否真正在被执行。 Watch log/development.log constantly to see what's happening. 经常观察log/development.log以了解发生了什么。 Most problems can be quickly resolved by examining what's been logged. 通过检查记录的内容,可以快速解决大多数问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我为nil:NilClass获取了一个未定义的方法“ each”。 尝试将帖子添加到用户个人资料 - I'm getting an undefined method `each' for nil:NilClass | Trying to add posts to users profiles 为什么我收到“ nil:NilClass的未定义方法&#39;map&#39;”错误 - Why I'm getting “undefined method `map' for nil:NilClass” error Rails - 为什么我为 nil:NilClass 得到未定义的方法“名称” - Rails - why I'm getting undefined method `name' for nil:NilClass Rails为nil:NilClass定义了未定义的方法“ each”…,但已定义 - Rails undefined method `each' for nil:NilClass…but it is defined 为什么我在 Rails 中为 nil:NilClass 得到未定义的方法“each”? - Whay am I getting undefined method `each' for nil:NilClass in Rails? 为什么我得到“未定义的方法`strip&#39;为nil:NilClass”当我没有调用strip方法时? - why am i getting “undefined method `strip' for nil:NilClass” when i'm not calling the strip method? 未定义的方法&#39;每个为nil:NilClass - undefined method 'each for nil:NilClass nil:NilClass的未定义方法“ each” - Undefined method `each' for nil:NilClass nil的未定义方法`each&#39;:NilClass? - undefined method `each' for nil:NilClass? nil:NilClass的未定义方法“ each” - undefined method `each' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM