Rails 新手遇到 Action Controller: Exception caught,请教

djks · 2011年12月11日 · 最后由 djks 回复于 2012年02月04日 · 5302 次阅读

提示信息是: The action 'show' could not be found for CommentsController

下面是 Comments 代码

class CommentsController < ApplicationController

http_basic_authenticate_with :name => "admin", :password =>"password" , :only => :destroy

def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment]) redirect_to post_path(@post) end

def destroy @url = @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@url) end

end

The action 'show' could not be found for CommentsController

你访问了一个页面,路由映射到 CommentsController#show 方法,但是这个方法未定义

我是参照这里教程http://guides.ruby.tw/rails3/getting_started.html#9 删除留言写的代码

下面这段是链接,请问这样是对的吗?

<%= link_to 'Destroy Comment', [comment.post, comment], :confirm => "Are you sure?",:method => :delete %>

#2 楼 @djks 感觉是 jquery-ujs 没触发,查看源码,把 html head 部分和这个 Destroy Comment 链接的 HTML 代码放出来看看

#2 楼 @djks 这一段是链到 destroy 这个 action 的。如果是这里出了问题,可能是你没有正确加了 jquery_ujs (jquery_rails)。

我发现我这里不能打开 /assets/all.css /assets/defaults.js

html 代码:

Blog

Destroy Comment

#4 楼 @ashchan 我查看 Gemfile 里面有 gem 'jquery-rails' 这一项是不是就加载了?

#6 楼 @djks 你还需要确定 app/assets/javascripts/application.js 里有以下两行:

//= require jquery
//= require jquery_ujs

#7 楼 @ashchan // //= require jquery //= require jquery_ujs //= require_tree .

这个文件里面有你说的代码

默认的 css 和 javascript 应该叫做 application.css 和 application.js,文件在 app/assets 下面的子目录可以找到,应该链接这两个

@djks 查看 你的 application.js , 里面 require jquery-ujs

#9 楼 @Rei 果然是这样问题,太感谢了 发现问题所在之后感觉这个问题简单了

#10 楼 @wxianfeng 是有 require jquery-ujs 的 只是我的 application.html.erb 里面加载不对 系统默认加载的名称不对

感谢@Rei @ashchan @wxianfeng 没有你们的帮助,这个问题估计我要弄很久才会明白

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