【发布时间】:2014-10-12 15:48:03
【问题描述】:
我是 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
来源在哪里:
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
而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
和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
知道什么可能导致这个问题吗? 我应该在哪里寻找?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4