ActionView :: Template :: Error(nil:NilClass的未定义方法“标题”)

时间:2019-08-31 12:43:32

标签: ruby-on-rails

我正在尝试发表博客文章,并收到了Template :: Error。我进行了搜索,我知道这是一个简单的解决方法,但无法弄清楚。我不知道自己缺少什么,因此非常感谢您的帮助。

articles_controller

 class ArticlesController < ApplicationController
 def new
@article = Article.new   end 
 def create 
@article = Article.new(article_params)
if  @article.save
  flash[:success] = "Article was successfully created"
  redirect_to article_path(@article)
else
  render 'new'
end    end 
 def show       end 
 def edit       end 
 def update
if @article.update(article_params)
  flash[:success] = "Article was updated"
  redirect_to article_path(!article)
else
  flash[:success] = "Article was not updated"
  render 'edit'
end    end 
 def index
@articles = Article.all   end 
 def destroy    @article.destroy    flash[:success] = "Article was deleted"    redirect_to articles_path   end 

  private
def article_params
  params.require(:article).permit(:title, :description)
end 

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

show.html.erb

<h2 align="center">Title: <%= @article.title %></h2>
<div class="well col-xs-8 col-xs-offset-2">
 <h4 class="center"><strong>Description:</strong></h4>
 <hr>
 <%= @article.description %>
<%= link_to "Edit", edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

1 个答案:

答案 0 :(得分:1)

@article动作中设置show

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

或使用before_action回调:

class ArticlesController < ApplicationController
  before_action :set_article, only: :show

  ...
end