[英]NoMethodError undefined method ` ' for nil:NilClass
I'm novice with rails and starting with http://guides.rubyonrails.org/getting_started.html stucked in 5.7 Showing Articles
with following error: 我是Rails的新手,从http://guides.rubyonrails.org/getting_started.html陷入
5.7 Showing Articles
,出现以下错误:
NoMethodError in Articles#show
Showing /home/jakub/workspace/blog/blog/app/views/articles/show.erb where line #3 raised:
undefined method `title' for nil:NilClass
Where the source is : 来源是:
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
and articles_controller.rb
is: 而
articles_controller.rb
是:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def show
@article = Article.find(params[:id])
end
end
and rake routes
command brings: 和
rake routes
命令带来了:
Prefix Verb URI Pattern Controller#Action
welcome_contact GET /welcome/contact(.:format) welcome#contact
welcome_index GET /welcome/index(.:format) welcome#index
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / welcome#index
Any idea what might cause this issue? 任何想法可能导致此问题? Where should I look for ?
我应该在哪里找?
Your show
action is private, therefore the instance variable @post
cannot be used in the view. 您的
show
操作是私有的,因此实例变量@post
不能在视图中使用。 Move it to a public scope and the problem should be fixed. 将其移至公共场所,应解决此问题。
Move your show action to public block. 将您的表演动作移到公共区域。
undefined method `***' for nil:NilClass is actually shown when you are trying to access a object property which is actually not created yet. 当您尝试访问实际上尚未创建的对象属性时,实际上会显示nil:NilClass的未定义方法***。
Always check on view like this: 总是这样检查视图:
@atrile.title if @article.title.present?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.