遵循 Ruby-on-Rails 教程并获得“销毁用户"不起作用 [英] Following Ruby-on-Rails tutorial and getting 'destroy users' doesn't work

查看:33
本文介绍了遵循 Ruby-on-Rails 教程并获得“销毁用户"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了 Ruby on Rails 3.2 并一直在努力学习它.我一直在关注 RoR 3.0 教程 (http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#top),到目前为止一切顺利(是的,我知道有一个 3.2版本).

I've recently installed Ruby on Rails 3.2 and have been trying to learn it. I've been following along with the RoR 3.0 tutorial (http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#top) and so far it is going well (yes I know there's a 3.2 version).

目前我被困在第 10.4.2 节,该节教如何添加链接以销毁用户.它说要添加代码

Currently I am stuck on section 10.4.2 which teaches how to add a link to destroy users. It says to add the code

<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
                            :title => "Delete #{user.name}" %>

以及在 apps/view/layout/application/html/erb 中添加

As well as adding in apps/view/layout/application/html/erb

<%= javascript_include_tag :defaults %>

这似乎应该直接用于用户控制器中的 destroy 方法,正如教程所说,但它对我不起作用,我不知道为什么.它创建的链接仅指向/user/:id.我查看了 3.2 教程中的同一部分,它的方向完全相同(但没有 javascript 包含标记代码).我无法按照该教程进行操作.所以我不确定它为什么不工作或如何让它工作.

It seems like this should take it right to the destroy method in the user controller, as the tutorial says but it is not working for me and I cannot figure out why. The link it creates is just to /user/:id. I looked at the same section in the 3.2 tutorial and it is fairly the same directions (but does not have the javascript include tag code). I can't get it to work following that tutorial. So I am not sure why it is not working or how to get it to work.

所以我们很清楚,不是去这个 User 控制器中的 destroy 方法,而是去/user/:id 这是 show 方法.

So we are clear, rather than going to the destroy method in this User controller, it goes to /user/:id which is the show method.

推荐答案

删除资源(在您的情况下为用户)需要在页面中包含 jquery_ujs javascript 文件.看到一个 'show' 动作被调用是很常见的,因为没有 jquery_ujs 就不会发送指示 HTTP DELETE 动词的隐藏数据.

Deleting a resource (a user in your case) requires jquery_ujs javascript file to be included on a page. It is quite common to see a 'show' action being called, because without jquery_ujs is not sending the hidden data that indicates the HTTP DELETE verb.

尝试显式插入 jquery_ujs,如下所示:

Try to explicitly insert the jquery_ujs like follows:

<%= javascript_include_tag 'jquery_ujs' %>

看看会发生什么.

jquery_ujs 旨在成为... Ruby on Rails 框架的不显眼的脚本支持文件,但不是严格绑定到任何特定的后端.'.换句话说,它扫描文档,查看特殊的 data-* 属性并根据这些属性执行各种操作,例如,附加隐藏的 html 元素,执行 ajax 请求等.

jquery_ujs is designed to be '... unobtrusive scripting support file for the Ruby on Rails framework, but is not strictly tied to any specific backend.'. In other words, it scans the document, sees the special data-* attributes and performs various actions depending on these attributes, for example, appending hidden html elements, performing ajax requests, etc.

还要注意,为了使用jquery_ujs,也应该引用jquery(之前).

Also note, that in order to use jquery_ujs, jquery should be referenced too (before).

希望这会有所帮助.

这篇关于遵循 Ruby-on-Rails 教程并获得“销毁用户"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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