ArticlesController中的NameError #create undefined局部变量或方法`article_params&#39;对于#<articlescontroller:你=“”是什么意思?=“”article_path =“”

时间:2017-08-30 02:00:23

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

=“”

我在创建时遇到此错误文章:

错误:ArticlesController中的NameError #create 未定义的局部变量或方法`article_params&#39;为#你的意思? 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

2 个答案:

答案 0 :(得分:0)

将文章参数放在创建

之外
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 

答案 1 :(得分:0)

我认为创建操作未正确关闭,因此您在创建操作中使用了article_params方法,删除了&#39; end&#39;在最后一行并添加一个&#39;结束&#39;对于create动作,它的语法错误。像这样

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