Ruby on rails RubyonRails教程第5.11节文章中的命名错误
别挡道了:Ubuntu 12.04 64位,Ruby 2.0.0,Rails 4.1.0 我试图学习官方的RubyonRails教程,但是我被第5.11节卡住了。我见过有人在5.12上有问题,但不是5.11,所以我想我应该问一下 这是他们教你如何编辑文件的部分。现在,正如他们所说,我的edit.html.erb文件:Ruby on rails RubyonRails教程第5.11节文章中的命名错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,别挡道了:Ubuntu 12.04 64位,Ruby 2.0.0,Rails 4.1.0 我试图学习官方的RubyonRails教程,但是我被第5.11节卡住了。我见过有人在5.12上有问题,但不是5.11,所以我想我应该问一下 这是他们教你如何编辑文件的部分。现在,正如他们所说,我的edit.html.erb文件: <h1>Editing article</h1> <%= form_for :article, url: articles_path(@articl
<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 %>
<%= link_to 'Back', articles_path %>
但是,当我尝试编辑文件时,会出现以下错误:
oMethodError in Articles#edit
Showing /home/aespielberg/RoR/blog/app/views/articles/edit.html.erb where line #4 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #4):
<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>
Rails.root: /home/aespielberg/RoR/blog
Application Trace | Framework Trace | Full Trace
app/views/articles/edit.html.erb:4:in `block in _app_views_articles_edit_html_erb__4315770151411025849_69984046087300'
app/views/articles/edit.html.erb:3:in `_app_views_articles_edit_html_erb__4315770151411025849_69984046087300'
现在,我不确定这是为什么,因为本文继承了:
class Article < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
end
据我所知,它应该有.errors和.errors.any函数
和我的控制器:
class ArticlesController < ApplicationController
def new
@article = Article.new
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
private
def article_params
params.require(:article).permit(:title, :text)
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def edit
@article = Article.find(params[:id])
end
end
这就引出了两个问题:
为什么会发生这种错误?
解决这个问题的正确方法是什么?
这是人们常犯的错误,让我发疯。阅读错误消息中的内容:
undefined method `errors' for nil:NilClass
这与文章模型以及它有或没有什么方法无关。这与缺少一个可操作的文章模型有关。这就是为什么在nil:NilClass上调用错误,而不是在Article的某个实例上调用错误
您的@article变量没有被设置,或者在您的控制器中被设置为零,您还没有发布。这是人们经常犯的错误,它让我发疯。阅读错误消息中的内容:
undefined method `errors' for nil:NilClass
这与文章模型以及它有或没有什么方法无关。这与缺少一个可操作的文章模型有关。这就是为什么在nil:NilClass上调用错误,而不是在Article的某个实例上调用错误
您的@article变量未被设置,或者在您的控制器中被设置为完全为零,您尚未发布该变量。您必须将编辑和更新方法置于私有方法之上。低于private的任何内容都将是私有方法,而edit和update应该是public方法。您必须将edit和update方法置于private之上。低于private的任何内容都将是private方法,编辑和更新应该是public方法。新方法中没有活动类。只需在新方法的articles\u controller.rb中添加@article=article.new。新方法中没有活动类。只需在articles\u controller.rb的new method中添加@article=article.new即可。对不起,现在添加控制器……我只是按照教程或尝试添加控制器。对不起,现在添加控制器……我只是按照教程或尝试添加控制器。哦,我感觉太傻了。非常感谢。哦,我觉得自己好笨。非常感谢。