【问题标题】:ruby on rails - unable to delete (destroy)ruby on rails - 无法删除(销毁)
【发布时间】:2015-06-13 06:29:09
【问题描述】:

由于某种原因,我无法删除 ruby​​ on rails 中的记录。 不幸的是,我尝试了许多解决方案均无济于事。 (如How to delete / destroy a record in rails?

application.erb的内容是:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

当我在 html 中打开时,它只显示以下 css,它不显示上面的标签中的 js。

<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/statuses.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />

我的index.html.erb

 <h1>Listing statuses</h1>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Content</th>
          <th colspan="3"></th>
        </tr>
      </thead>

      <tbody>
        <% @statuses.each do |status| %>
          <tr>
            <td><%= status.name %></td>
            <td><%= status.content %></td>
            <td><%= status.id %></td>
            <td><%= link_to 'Show', status %></td>
            <td><%= link_to 'Edit', edit_status_path(status) %></td>
            <td><%= link_to 'Destroy', status , method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>

    <br>

    <%= link_to 'New Status', new_status_path %>

我目前正在对此进行故障排除,但无济于事,如果有人可以在此代码中找出任何错误,我将不胜感激。 :) js文件没有显示在页面的head部分并且destroy函数不起作用,更不用说confirm标签了。

更新:现在我收到此错误:

TypeError: 对象不支持此属性或方法 Rails.root:[位置]

如果我删除 '&lt;%= javascript_include_tag %&gt;' 它会起作用,虽然不是完全(即不会像第一次那样删除)

更新 2 当我点击销毁按钮时,它只是将我重定向到仅查看该特定状态,它不会销毁

update 3application.js 我有以下内容:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

更新 4 我已将 &lt;%= javascript_include_tag %&gt; 更改为 &lt;%= javascript_include_tag 'default', 'data-turbolinks-track' =&gt; true %&gt; 并加载了 js 文件,但是当我从源代码中单击该 js 文件时,出现路由错误。 有什么办法可以解决吗?

【问题讨论】:

  • 怎么不行?会发生什么?
  • 请在单击销毁操作后显示您的日志内容。
  • 查看您的日志。是否调用了正确的操作?查看正在传递的参数。它们是否符合操作中代码的预期?使用参数单步执行操作中的代码。保存对象时会发生什么?保存失败并返回false?如果是这样,是否存在验证失败?如果这些问题中的任何一个揭示了意外发生的事情,您可以尝试找出原因。这就是你的策略。
  • 看起来你没有在标签javascript_include_tag中包含任何javascript代码,这是通过链接&lt;a&gt;标签发送正确的删除请求所必需的。
  • 不想问明显的问题,但你的控制器代码在哪里?

标签: ruby-on-rails ruby destroy


【解决方案1】:

application.erb 中,您需要将application.js 文件包含在javascript_include_tag 中,如下所示:

<%= javascript_include_tag 'application' %>

假设您在application.js 中设置了jqueryjquery_ujs

【讨论】:

  • 确实,它们被包括在内,但是没有 javascript 显示在 html 中,只有 css 文件
  • @user3108623 你的 Gemfile 中有 therubyracer 吗?
  • 我有,它被注释掉了。
  • 取消注释,运行bundle install 并重新加载服务器。检查你是否有同样的问题。
  • 我运行它。问题似乎并不在于 ruby​​racer 本身。我现在得到一个 javascript 文件,但是当我点击它时,我得到:Routing Error: No route matches [GET] "/javascripts/default.js"
猜你喜欢
  • 1970-01-01
  • 2022-06-13
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多