簡體   English   中英

ActionView :: Template :: Error(nil:NilClass的未定義方法“標題”)

[英]ActionView::Template::Error (undefined method `title' for nil:NilClass)

我正在嘗試發表博客文章,並收到了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 %>

show動作中設置@article

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

或使用before_action回調:

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

  ...
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM