我在创建时遇到此错误文章:
错误:ArticlesController中的NameError #create 未定义的局部变量或方法`article_params'为#你的意思? article_path
我的代码:
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 :(得分: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