Getting the undefined `errors' method for nil: NilClass Ruby on Rails Guide

Well, I was looking for a question here just like mine, and I can’t find it, so I have to ask it myself. I follow the guide here: http://guides.rubyonrails.org/getting_started.html

I encountered this error: "undefined method" errors "for nil: NilClass" for my page "Edit article".

Here is my extracted source:

 <h1>Editing article</h1>
 <%= form_for :article, url: articles_path(@article), method: :patch do |f| %>
   <% if @article.errors.any? %>
     <div id="error_explanation">
       <h2><%= pluralize(@article.errors.count, "error") %> prohibited
            this article from being saved: </h2>

And here is my trace:

app / views / articles / edit.html.erb: 4: in a block in _app_views_articles_edit_html_erb ___ 778692675__618464548

app / views / articles / edit.html.erb: 3: in _app_views_articles_edit_html_erb ___ 778692675__618464548

Here is my action controller

class ArticlesController < ApplicationController
def new
    @article = Article.new
end

def edit
    @articles = Article.find(params[:id])
end

def create
    @article = Article.new(article_params)

    if @article.save
        redirect_to @article
    else
        render 'new'
    end
end

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

def index
    @articles = Article.all
end

def update
    @article = Article.find(params[:id])

    if @article.update(article_params)
        redirect_to @article
    else
        render 'edit'
    end
end

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


 end

And here is my edit.html.erb

<h1>Editing article</h1>

<%= form_for :article, url: articles_path(@article), method: :patch do |f| %>
  <% if @article.errors.any? %>
    <div id="error_explanation">
  <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article 
        from being saved: </h2>
     <ul>
        <% @article.errors.full_messages.each do |msg| %><li><%= msg %></li>
     <% end %>
     </ul>
     </div>
  <% end %>


  <p>
    <%= f.label :title %><br>
<%= f.text_field :title %>
  </p>

  <p>
<%= f.label :text %><br>
<%= f.text_area :text %>
  </p>

  <p>
<%= f.submit %>
  </p>
<% end %>
+4
4

-, , , , Arup, :

"undefined ` errors ' nil: NilClass

, , , nil. , @instance_variable, .

, , , undefined . , - undefined; , .

-

Fix

, Arup, @instance variable, edit, :

#app/controllers/articles_controller.rb
Class ArticlesController < ApplicationController
   def edit
       @article = Article.find params[:id]
   end
end

#app/views/articles/edit.html.erb
<%= @article.errors %>

- , , :

#app/views/articles/edit.html.erb
<%= form_for @article do |f| %>
   # ...
<% end %>
+4

, .

<% if @article.errors.any? %>

<% if @articles.errors.any? %>

#edit @articles not @article. @article, . , <% if @article.errors.any? %> , #edit

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

, nil, , Ruby. . @articles, @article, , , nil. nil.errors , .

+3

, @article nil (null)

, :

def new
  @article = Article.new
end

-, .

+2

def new

@article = Article.new

Since your copy of the article is nil.

0
source

Source: https://habr.com/ru/post/1545131/


All Articles