我是Rails的新手,我根据rubyonrails.org教程开始制作一个Web应用程序。
我的应用程序是一个带有文章的博客。.我实现了创建和编辑功能,这些功能运行得很好,但是在尝试访问http://localhost:3000/articles/2/edit
以编辑文章时突然出现错误。错误是ActionController::ParameterMissing in ArticlesController#edit param is missing or the value is empty: articles
这是我的红宝石代码:
类ArticlesController 错误警报所针对的行是 谢谢您的帮助def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def show
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
params.require(:articles).permit(:title, :text)
我真的不知道错误可能在哪里,因为2分钟前一切都还好...
您正在尝试使用edit方法更新文章。因此,当您导航到“ articles / 2 / edit /”时,它将尝试更新文章2。但是您没有传递任何参数。
我认为您可能想要的是:
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
我知道已经晚了,但是我希望此解决方案可以帮助某人。在ArticleController中需要添加以下行:
def edit
@article = Article.find(params[:id])
end
和这个...
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end