Reputation: 15297
I'm novice with rails and starting with http://guides.rubyonrails.org/getting_started.html stucked in 5.7 Showing Articles
with following error:
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:
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:
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 ?
Upvotes: 1
Views: 2684
Reputation: 5144
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.
Always check on view like this:
@atrile.title if @article.title.present?
Upvotes: 1
Reputation: 2469
Your show
action is private, therefore the instance variable @post
cannot be used in the view. Move it to a public scope and the problem should be fixed.
Upvotes: 3