Rails <Rails Guides>里 Destroy Comments 的问题

dsmylv · 2013年06月23日 · 最后由 alicebobby 回复于 2024年03月13日 · 5008 次阅读

按照 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
1 楼 已删除

贴一下你的 app/views/comments/_comment.html.erb

#2 楼 @blacktulip

<% @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 %>

#3 楼 @dsmylv config/route.rb 里面 comment 挂错位置了吧?

#4 楼 @linjunhalida config/route.rb:::::::::

resources :posts do 
  resources :comments
end

#5 楼 @dsmylv 恩,回头看了一下,是点击删除链接变成 get 请求了,你:action => "show",这行应该是 delete 吧?

#6 楼 @linjunhalida 应该是:action => "destroy"

#6 楼 @linjunhalida 可 post 没有 delete 方法呀。

#7 楼 @icesky 都不对吖...5555

<%= link_to '删除', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete, :action => "destroy", :controller => "comment" -%> 要删除的是 comment,所以 controller 是 comment

#10 楼 @icesky 还是不对吖...5555

<%= link_to '删除', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete, -%> 这样呢,你现在的写法我没怎么用过,再不行的话把错误信息发出来吧

我以为:action => ..., : controller => ...这种写法早就废弃了,现在不是都写成 post_comment_path 之类的吗? 教程要是那么写,可能那个教程有点儿过时了。

:controller => '', :action => ''給拿掉,妳就會正常了 ....

#10 楼 @icesky controller 也不是這樣寫的啊 XDD ... 應該要:controller => 'comments'才對呀

link_to 第二個參數是 URL ...

所以如果要改成用:controller等參數的話,應該是 link_to("刪除", { :controller => "comments", :action => "destroy", :id => comment.id }) 才對

但是這邊卻又用到了 Resources 嵌套,但是在以前這種寫法我記得好像不能用在嵌套上 ...

#15 楼 @hechian 谢谢提醒了,没用过这种写法,不过看过,细节没记起来^_^

#16 楼 @icesky 你跟 @iceskysl 是同一人嗎?哈哈

#17 楼 @hechian 不是,可能一不小心把他想要的 username 注册了

#18 楼 @icesky 可他比你還要早註冊呢= ="

#19 楼 @hechian2011年12月01日注册,他2011年12月22日注册

#20 楼 @icesky 這下換我眼花了 Orz

呵,很简单的呀,你自己看下你的 app/view/layout/application.html.erb 文件吧,rails3 的问题。说到这里,相信许多人就应该想起来了吧。前二天我哥在学习过程中,也遇到了。delete 失效,走的是 get 方法,呵

#21 楼 @hechian #16 楼 @icesky 我的 id 用肋好久啦,呵呵~

#23 楼 @iceskysl 又是好久不見了 XD

有可能是 rails.js 不对,看看 rails 生成的 html,如果对应的链接上面有 data-method="delete" 的话,基本可以肯定是 js 的问题

匿名 #27 2013年06月29日

应该是 js 的问题 确保 application.html.erb 中

<%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

和 application.js 中加载 js

我发誓我已经使用我的 ID 很长时间了。 buildnow gg

需要 登录 后方可回复, 如果你还没有账号请 注册新账号