【问题标题】:undefined method `each' for nil:NilClass - help needednil:NilClass 的未定义方法“each” - 需要帮助
【发布时间】:2020-01-24 03:29:28
【问题描述】:

我正在自学 Ruby on Rails,并尝试通过教程构建博客。我看到了其中一些答案,似乎我的语法是正确的,但我在标题中遇到了错误。

这是我的articles_controller 代码:

class ArticlesController < ApplicationController
    before_action :set_article, only: [:edit, :update, :show, :destroy]

    def index
        @articles = Article.paginate(page: params[:page], per_page: 5)
    end

    def new
        @article = Article.new
    end

    def edit
    end

    def create
        @article = Article.new(article_params)
        @article.user = User.first
        if @article.save
            flash[:success] = "Article was successfully created"
            redirect_to article_path(@article)
        else
            render 'new'
        end
    end

    def update
        if @article.update(article_params)
            flash[:success] = "Article was updated"
            redirect_to article_path(@article)
        else
            flash[:danger] = "Article was not updated"
            render 'edit'
        end
    end

    def show

    end

    def destroy
        @article.destroy
        flash[:danger] = "Article was deleted"
        redirect_to articles_path
    end

    private
        def set_article
            @article = Article.find(params[:id])
        end

        def article_params
            params.require(:article).permit(:title, :description)
        end
end

这是我的 index.html.erb:

<h1 align="center">Listing all articles</h1>
<% @articles.each do |article| %>
 <div class="row">
  <div class="col-xs-8 col-xs-offset-2">
   <div class="well well-lg">
    <div class="article-title">
     <%= link_to article.title, article_path(article) %>
    </div>
    <div class="article-body">
     <%= truncate(article.description, length: 100) %>
</div>
    <div class="article-meta-details">
     <small>Created by: <%= article.user.username if article.user %>, <%= time_ago_in_words(article.created_at) %> ago, last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
    </div>
    <div class="article-actions">
     <%= link_to "Edit", edit_article_path(article), class: "btn btn-xs btn-primary" %>
     <%= link_to "Delete", article_path(article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
    </div>
   </div>
  </div>
 </div>
<% end %>

如果能得到任何帮助,我将不胜感激。谢谢。

【问题讨论】:

  • 1.你什么时候得到错误?参观什么路线? 2. 堆栈跟踪会很有帮助 3. 如果问题可能存在,请同时发布其他模板
  • 请添加您正在使用的错误和 Rails 版本的堆栈跟踪
  • 根据您提供的信息,听起来@articles 在您的index 方法中被设置为nil。您的数据库中是否存在您的 articles 表?
  • @wvengen 当我使用 "rails s" 启动服务器并登录到 localhost:3000 时,会发生此错误。
  • @Vasfed 我正在使用 Rails 5.2.4.1。

标签: ruby-on-rails ruby


【解决方案1】:

我认为您在index 方法中的语法是错误的。

@articles = Article.paginate(page: params[:page], per_page: 5)

应该是

@articles = Article.all.paginate(page: params[:page], per_page: 5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多