ArticlesController 中的 NameError#create undefined local variable or method `article_params' for #<ArticlesController:Did you mean? article_path
NameError in ArticlesController#create undefined local variable or method `article_params' for #<ArticlesController:Did you mean? article_path
我在创建文章时遇到此错误:
错误:ArticlesController 中的名称错误#create
未定义的局部变量或方法 `article_params' for # Did you mean? 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
将文章参数放在创建之外
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
我认为创建操作没有正确关闭,因此在创建操作中有 article_params 方法,删除最后一行的 'end' 并添加一个 '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
我在创建文章时遇到此错误:
错误:ArticlesController 中的名称错误#create 未定义的局部变量或方法 `article_params' for # Did you mean? 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
将文章参数放在创建之外
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
我认为创建操作没有正确关闭,因此在创建操作中有 article_params 方法,删除最后一行的 'end' 并添加一个 '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