Rails 3 link_to delete destory 方法调用show 方法? [英] Rails 3 link_to delete destory method calls show method?

查看:48
本文介绍了Rails 3 link_to delete destory 方法调用show 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我已经看过几篇关于此的帖子,但我仍然遇到调用 :method => 'delete' 并被定向到我的控制器的 show 方法的问题.destroy 方法按预期工作,因为它删除了评论,但是在请求完成后,它会在 GET 上抛出 404.代码如下:

Hey I have seen several posts on this, but I am still having issues with calling :method => 'delete' and getting directed to the show method of my controller. The destroy method is working as expect, in that it deletes the comment, but after the request is done, it throws a 404 on GET. Here is the code:

<%= link_to 'delete', "/events/#{@event.id}/comments/#{comment.id}.js", 
        :confirm => 'Are you sure?', 
        :method => :delete, 
        :remote => true %>

这是控制器方法:

def destroy 
  @comment = @event.comments.find(params[:id])
  @comment.destroy
  redirect_to do |format|
    format.html # redirect_to @event, :notice => "comment deleted" }
    format.js { render 'destroy.js.erb' }
  end
end

我听说这可能是由于没有使用 button_to,但我尝试过使用 button_to 而不是 link_to,但这也做同样的事情.

I've hear that this could be due to not using button_to, but I have tried using button_to as opposed to link_to but this does the same thing.

我还听说这可以解决您在设置中使用 jquery 的方式的一些问题,但我觉得我对此表示怀疑,但这里是我调用 jquery 以防万一(application.html.erb):

I've also heard that this could be do to some problems with the way you are using jquery in your set up, but I feel like I doubt that, but here is how I call in jquery just in case (application.html.erb):

<%= javascript_include_tag 'jquery-1.5.2.min.js', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery-ui-1.8.17.custom.min.js' %>

当我观看 rails 服务器输出时,我看到它说:

When i watch rails server output I see that it's says:

Redirected to http://0.0.0.0:3000/events/1/comments/35
Completed 302 Found in 132ms
ACTION NAME application


Started GET "/events/1/comments/35" for 127.0.0.1 at Wed Feb 08 16:31:43 -0800 2012

AbstractController::ActionNotFound (The action 'show' could not be found for CommentsController):

感谢您的帮助!

推荐答案

Rails 3.1 及更高版本为我们清理了很多问题……rails.js 不见了.Jquery_ujs 是 gem 的一部分,但该文件不应在您的 javascript 目录中:具体来说,我的 jquery_ujs 文件很旧并且仍然存在"在我的 assets/javascript 目录中.它仍然有被弃用的 .live 方法.删除文件!!
保留 gem 'jquery-rails', '~> 2.1' # REF: https://github.com/rails/jquery-ujs保持//= require jquery 和//= require jquery_ujs 和//= require_tree .在您的 application.js 文件中确保更新 gem 和 bundle install 以获取最新的 gem.如果 javascript 错误被抛出,因为对我来说是偷偷摸摸"的情况,link_to js 的其余部分将不起作用.现在删除和确认对话框工作正常.一个旧的 jquery_ujs 是罪魁祸首.

Rails 3.1 and greater cleans a lot of this up for us...rails.js is gone. Jquery_ujs is part of the gem, but the file should NOT be in your javascript directory: specifically, my jquery_ujs file was old and still "living" in my assets/javascript directory. It still had .live methods which are deprecated. Delete the file!!
Keep the gem 'jquery-rails', '~> 2.1' # REF: https://github.com/rails/jquery-ujs Keep the //= require jquery and //= require jquery_ujs and //= require_tree . In your application.js file Make sure you update the gem and bundle install to get the latest gem. If a javascript error is thrown as was the "sneaky" case for me, the rest of link_to js will not work. Now the delete and the confirmation dialog are working fine. An old jquery_ujs was the culprit.

这篇关于Rails 3 link_to delete destory 方法调用show 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆