如何删除/销毁rails中的记录? [英] How to delete / destroy a record in rails?

查看:37
本文介绍了如何删除/销毁rails中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于 railsguide 构建一个 rails 应用程序

I'm building a rails app based off of the railsguide

 http://guides.rubyonrails.org/getting_started.html

它在 erb 中调用的语法是...

The syntax it calls for in the erb is...

<td><%= link_to 'Destroy', article_path(article),
          method: :delete,
          data: { confirm: 'Are you sure?' } %></td>

出于某种原因,这读作

https://localhost:3000/articles/[#]

其中# 是要删除的给定记录.换句话说,我想删除一条记录,它会将我的代码解释为显示所述记录.

where # is the given record to delete. In other words, I want to delete a record, and it interprets my code as showing said record.

我可能做错了什么?

更多信息

这是动态生成的

<td>
  <a rel="nofollow" data-method="delete" href="/articles/2">Destroy</a>
</td>

application.html.erb 有以下内容...

application.html.erb has the following...

 <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
 <%= csrf_meta_tags %>

控制器定义

def destroy
@article= Article.find(params[:id])
@article.destroy

redirect_to articles_path
end

与在 application.html.erb 中使用应用程序"相关的错误

Error associated with using 'application' in application.html.erb

ExecJS::ProgramError in Welcome#index

Showing E:/scabase/app/views/layouts/application.html.erb where line #6     raised:

TypeError: Object doesn't support this property or method
(in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/turbolinks-  2.5.3/lib/assets/javascripts/turbolinks.js.coffee)

推荐答案

这是完全正常的.在 REST 世界中,请求使用动词(GET、DELETE 等)和标识资源的 URI 的组合.如果你对同一个资源(即同一篇文章)做不同的事情,那么只有动词会发生变化.对象的显示页面使用 GET,更新操作使用 PATCH(rails 4.1 之前的 PUT),销毁记录使用 DELETE

This is completely normal. In the REST world a request uses a combination of a verb (GET, DELETE etc) and a URI that identifies the resource. If you are doing different things to the same resource (i.e. the same article) then only the verb changes. The show page for an object uses GET, the update action uses PATCH (PUT prior to rails 4.1) and destroying a record uses DELETE

实际上链接总是导致 GET 请求,甚至表单只允许 GET 或 POST,所以 rails 模拟其余的方法 - 单击带有 data-method=destroy 属性的链接会创建一个表单使用值为 DELETE 的隐藏 _method 输入发布到 URL.这是通过一些包含在新生成的 Rails 应用程序中的 javascript 完成的.

In practice links always result in GET requests and even forms only allow GET or POST, so rails emulates the remaining methods - clicking on a link with a data-method=destroy attributes creates a form that posts to the URL with a hidden _method input with value DELETE. This is done via some javascript that is including in newly generated rails apps.

你有

<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>

并且您已经指出您在加载 default.js 时遇到错误 - 我猜您正在遵循一组旧的说明,因为在现代应用程序中(自 rails 3.1 起)这应该是

and you've indicated that you're getting an error loading default.js - I'd guess that you're following an old set of instructions because in a modern app (since rails 3.1) this should be

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

您的 application.js 文件还应包含(除其他外)

Your application.js file should in addition contain (among other things)

//= require jquery
//= require jquery_ujs

(你也可以使用原型或其他js库)

(You can use prototype or other js libraries too)

这篇关于如何删除/销毁rails中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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