【发布时间】:2017-12-11 10:49:36
【问题描述】:
我正在关注这个tutorial 来构建一个简单的博客端。
但是,我无法实现教程中显示的删除功能。
给出的步骤如下:
让我们在 ArticlesController 中定义 destroy 方法:
使用 params[:id] 在数据库中查找文章
对该对象调用 .destroy
重定向到文章索引页面
现在就自己动手并测试一下。
这是我的代码:
class ArticlesController < ApplicationController
include ArticlesHelper
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
@article.save
flash.notice = "Article '#{@article.title}' Created!"
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
show.html.erb
<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<%= link_to "<< Back to Articles List", articles_path %>
<%= link_to "delete", article_path(@article), method: :delete %>
article_helper.rb
module ArticlesHelper
def article_params
params.require(:article).permit(:title, :body)
end
end
我在网上浏览了其他解决方案,但没有看到任何我可能遗漏的代码。
在我的例子中,点击删除链接只会导致页面刷新。
【问题讨论】:
-
为什么是
private?向上移动 -
谢谢,我在 github 上关注修改后的教程,我按照较新的教程更新了代码,但它仍然不起作用。
-
错误是什么?
-
也添加日志
-
没有,点击删除按钮后页面重新加载,没有任何反应。我对 Rails 很陌生,日志是反映在文件中还是直接反映在控制台中?此外,如果有帮助,其他方法也可以正常工作。
标签: ruby-on-rails ruby