0

當我創建我的文章,我得到這個錯誤? 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 

回答

0

放物品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

0

我想創建行動沒有正確關閉,因此你有在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 
相關問題