ArticlesController中的NameError#为#

错误:NameError在ArticlesController#创建 未定义的局部变量或方法`article_params'为#你的意思是article_path

问题描述:

当我创建我的文章,我得到这个错误? article_pathArticlesController中的NameError#为#<ArticlesController创建未定义的局部变量或方法`article_params':你的意思是?</p> <p>错误:NameError在ArticlesController#创建 未定义的局部变量或方法`article_params'为#你的意思是article_path

错误的图像:enter image description here

我的代码:

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    end 

    def create 
    @article = Article.new(article_params) 
    if @article.save 
     flash[:notice] = "Article was submitted succsefully" 
     redirect_to (@article) 
    else 
     render :new 
    end 

    private 

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

放物品PARAMS之外的创建

class ArticlesController < ApplicationController 

    def new 
    @article = Article.new 
    end 

    def create 
    @article = Article.new(article_params) 
    if @article.save 
     flash[:notice] = "Article was submitted succsefully" 
     redirect_to (@article) 
    else 
     render :new 
    end 
    end 

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

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

,但我得到这个错误 –

+0

未知的动作 行动“秀”不能为ArticlesController –

+0

找到我的回答只是增加了如何在文章控制器添加show动作 – widjajayd

我想创建行动没有正确关闭,因此你有在create动作中的article_params方法,删除最后一行的'end',并向create动作添加一个'end',它的语法错误。这样

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    end 

     def create 
     @article = Article.new(article_params) 
     if @article.save 
      flash[:notice] = "Article was submitted succsefully" 
      redirect_to (@article) 
     else 
      render :new 
     end 
     end 

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