按照 Rails Guides 实现一个博客系统 http://guides.ruby.tw/rails3/getting_started.html
问题是:"删除评论"模块,点击"Destroy"超链接错误:
Unknown action
The action 'show' could not be found for CommentsController
CommentsController.rb:::
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
<% @post.comments.each do |comment| %>
<p><%= comment.user -%>:<%= comment.body %> </p>
<% if session[:user_name] == "rails" -%>
<div class="edit_link">
<%= link_to '删除', [comment.post, comment], :confirm => 'Are you sure?',
:method => :delete,
:action => "show",
:controller => "post" -%>
</div>
<% end -%>
<% end %>
#4 楼 @linjunhalida config/route.rb:::::::::
resources :posts do
resources :comments
end
<%= link_to '删除', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete, :action => "destroy", :controller => "comment" -%> 要删除的是 comment,所以 controller 是 comment
<%= link_to '删除', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete, -%> 这样呢,你现在的写法我没怎么用过,再不行的话把错误信息发出来吧
我以为:action => ..., : controller => ...这种写法早就废弃了,现在不是都写成 post_comment_path 之类的吗? 教程要是那么写,可能那个教程有点儿过时了。
呵,很简单的呀,你自己看下你的 app/view/layout/application.html.erb 文件吧,rails3 的问题。说到这里,相信许多人就应该想起来了吧。前二天我哥在学习过程中,也遇到了。delete 失效,走的是 get 方法,呵
有可能是 rails.js 不对,看看 rails 生成的 html,如果对应的链接上面有 data-method="delete" 的话,基本可以肯定是 js 的问题
应该是 js 的问题 确保 application.html.erb 中
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
和 application.js 中加载 js