我正在遵循rails教程(http://tutorials.jumpstartlab.com/projects/blogger.html#blogger-2),创建一个简单的博客。在其中一个练习中,它要求我为我的Articles_Controller实现破坏方法(文章是博客帖子结构的模型)。
我已经实现了delete函数,但是之后,当尝试redirect_to article_path(@article_path)时,它找不到记录(当然,它被删除了)。我想知道如何redirect_to索引页面?
删除一篇文章后,我将得到rails错误页面,并:
error: ActiveRecord::RecordNotFound in ArticlesController#show 我的应用程序/控制器/文章_控制器. my:
def destroy
@article = Article.find(params[:id])
flash.notice = "Article '#{@article.title}' destroyed."
redirect_to article_path(@article)
@article.destroy
endArticleController#show中定义的方法
def show
@article = Article.find(params[:id])
end 发布于 2014-03-30 10:44:30
可以使用redirect_to articles_path重定向到索引路径。
因此:
def destroy
begin
@article = Article.find(params[:id])
if @article.destroy
redirect_to articles_path, notice: "Article '#{@article.title}' destroyed."
else
redirect_to article_path(@article), alert: "Article could not be destroyed."
end
rescue ActiveRecord::RecordNotFound
redirect_to articles_path, alert: "Article with id '#{params[:id]}' not found"
end
end发布于 2014-03-30 10:50:49
如果要重定向到应用程序的索引页,请执行以下操作。你可以这样做。
def destroy
@article = Article.find(params[:id])
@article.destroy
flash.notice = "Article '#{@article.title}' destroyed"
redirect_to root_index
endhttps://stackoverflow.com/questions/22742925
复制相似问题