【发布时间】:2012-08-06 11:39:22
【问题描述】:
我有一个包含文章和评论的应用程序(教程)。一篇文章有_许多评论。评论属于文章。我在删除文章评论时遇到问题。以下是有问题的文件:
app/views/cmets/_comment.html.erb
<%= div_for comment do %>
<h3>
<%= comment.name %> <<%= comment.email %>> said:
<span class='actions'>
<%= link_to 'Delete', [@article, comment], confirm: 'Are you sure?', method: :delete %>
</span>
</h3>
<%= comment.body %>
<% end %>
评论控制器
before_filter :load_article
def create
@comment = @article.comments.new(params[:comment])
if @comment.save
redirect_to @article, :notice => 'Thanks for your comment'
else
redirect_to @article, :alert => 'Unable to add comment'
end
end
def destroy
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to @article, :notice => 'Comment deleted'
end
private
def load_article
@article = Article.find(params[:article_id])
end
routes.rb
resources :articles do
resources :comments
end
问题是当我在地址 localhost:3000/articles/1 并尝试删除评论时。我没有被重定向到文章显示操作,而是在地址 localhost:3000/articles/1/cmets/3 处收到此错误:
Unknown action
The action 'show' could not be found for CommentsController
非常感谢任何帮助, 谢谢, 迈克
【问题讨论】:
-
这意味着请求不是作为
DELETE请求发送的,它会映射到destroy操作,而是作为GET,它映射到show操作。请添加为您的删除链接创建的实际 HTML,以及显示 HTTP 请求和参数的日志文件的输出。 -
Delete
link_to与一些 javascript 帮助一起使用,因此如果您在浏览器中禁用了脚本,没有在布局中包含 javascript 文件或有一些 js 错误,链接将导致显示操作而不是摧毁一个。你看到“你确定吗?”点击链接后确认? -
是的,“你确定吗?”没有出现。我将
<%= javascript_include_tag :defaults %>更改为<%= javascript_include_tag :application %>并解决了问题,但我不知道为什么。
标签: ruby-on-rails-3