javascript - Rails 4.1.8 link_to 删除,未应用 JavaScript

标签 javascript jquery html ruby-on-rails ruby-on-rails-4

我正在关注 Rails 入门指南。

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

我在尝试使用 link_to“删除”方法销毁文章时遇到问题。服务器控制台显示未使用 DELETE 而是调用 GET 方法,因此不遵循路由。 在指南的第 5.13 节中...

articles_controller.rb

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

    redirect_to articles_path
  end

关于这个主题有很多类似的问题,但我还没有找到解决为什么这不起作用的方法。 (我知道 button_to 确实有效,但指南引用 link_to 一定是有原因的)

我检查了我的 application.js 文件并确保包含 jquery 和 jquery-ujs:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree

控制台输出:

Started GET "/articles" for 127.0.0.1 at 2016-02-05 15:27:43 +1000
Processing by ArticlesController#index as HTML
  Article Load (1.0ms)  SELECT "articles".* FROM "articles"
  Rendered articles/index.html.erb within layouts/application (4.9ms)
Completed 200 OK in 52ms (Views: 50.0ms | ActiveRecord: 1.0ms)

布局文件application.html.erb

  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag '**defaults**', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

使用浏览器开发工具,似乎正确包含了 JS 文件。我还检查了不同的浏览器并确保 javascript 在浏览器上运行。

这里是浏览器<head>部分:

<head>
  <title>Blog</title>
  <link data-turbolinks-track="true" href="/assets/articles.css?body=1" media="all" rel="stylesheet">
<link data-turbolinks-track="true" href="/assets/welcome.css?body=1" media="all" rel="stylesheet">
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet">
  <script data-turbolinks-track="true" src="/javascripts/**defaults**.js">    </script>
  <meta content="authenticity_token" name="csrf-param">
<meta content="TwTtY9tuU/f7W5kWKkxNITD+KjuZ3zcTVD6b+8IkihA=" name="csrf-token">
</head>

我已尝试按照建议将 application.js 文件更改为以下内容 here !

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

<%= yield %>

</body>
</html>

得到如下错误: enter image description here

还有什么我可以尝试让 link_to 按预期工作的吗?我知道 rails 使用 javascript 作为 link_to DELETE 和确认框,但它没有显示。

编辑:添加了使用 link_to 的 View 文件。

index.html.erb
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article), method: :delete,
          data: { confirm: 'Are you sure?' } %></td>
    </tr>

最佳答案

错误似乎是 ExecJS没有为您的系统正确配置:

ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.


如果您使用的是 Windows,您应该 download & install nodeJS

如果您使用的是 Linux/Mac,您应该将 therubyracer/execjs 添加到您的 Gemfile:

#Gemfile
gem 'execjs'       
gem 'therubyracer' 

ExecJS and could not find a JavaScript runtime

关于javascript - Rails 4.1.8 link_to 删除,未应用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35217269/

相关文章:

javascript - 如何合并数组中的重复对象值然后输出列表?

javascript - Bootstrap 选项卡将所有内容添加到 1 个选项卡/药丸

jquery - 检查正数或负数

jquery - 动态突出显示列表项而不突出显示其子项

PHP Switch case问题,总是返回错误

html - 如何叠加图像

javascript - Firefox 对数组排序错误

javascript - 添加下拉值的新输入字段

javascript - 物化模式关闭时如何清除输入字段?

html - asp 文本框必填字段验证器消息定位