rails undefined method `title' for nil:NilClass

1. undefined method `title' for nil:NilClass

Showing C:/Sites/blog/app/views/articles/show.html.erb where line #3 raised:

undefined method `title' for nil:NilClass
<p><strong>Title:</strong><%= @article.title %></p>


class ArticlesController < ApplicationController

def new

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


solved methond: move private method to the end.

class ArticlesController < ApplicationController

def new

end


def create

 @article = Article.new(article_params)

 

 @article.save

 redirect_to @article

end

def show

  @article = Article.find(params[:id])

end


private

 def article_params

   params.require(:article).permit(:title, :text)

 end

end

你可能感兴趣的:(rails undefined method `title' for nil:NilClass)