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 %>