我有一个应用程序(一个教程),其中有文章和评论。一篇has_many评论文章。评论belongs_to一篇文章。我在删除一篇文章的评论时遇到了问题。以下是有问题的文件:
app/views/comments/_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 %>CommentsController
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])
endroutes.rb
resources :articles do
resources :comments
end问题是当我在address localhost:3000/articles/1尝试删除评论时。而不是重定向到文章显示操作,而是在address localhost:3000/articles/1/comments/3上得到这个错误
Unknown action
The action 'show' could not be found for CommentsController任何帮助都非常感谢,谢谢,迈克
发布于 2012-08-06 12:48:45
这里有两个基本选项,因为大多数浏览器中的链接只能发送GET请求。
第一个选项是将java-script默认文件包含到页面中。
<%= javascript_include_tag :defaults %> #this mocks a delete action by modifying the request automatically第二,也是更好的选择是使用button_to。首先,一个链接到一个地方和一个按钮之间有一个逻辑的分离来做一些事情。删除绝对是一个动作。此外,按钮后面没有蜘蛛,所以不会有意外的调用。
<%= button_to 'delete', @comment, :method => :delete %> =========编辑的完整性=======如果您担心的链接和按钮看起来不一样,一个简单的解决方案是我们用户界面风格的所有链接和按钮完全相同。
https://stackoverflow.com/questions/11827533
复制相似问题