【问题标题】:Rails 4.1.8 link_to Delete, JavaScript not being appliedRails 4.1.8 link_to 删除,未应用 JavaScript
【发布时间】:2016-02-05 05:59:41
【问题描述】:

我正在关注 Rails 入门指南。

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

我在尝试使用 link_to 'Delete' 方法销毁文章时遇到问题。服务器控制台显示没有使用 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 正在浏览器上运行。

这里是浏览器&lt;head&gt;部分:

<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>

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

<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>

得到以下错误:

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

编辑:添加了正在使用 link_to 的视图文件。

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>

【问题讨论】:

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


    【解决方案1】:

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

    ExecJS 允许您从 Ruby 运行 JavaScript 代码。它会自动选择可用于评估 JavaScript 程序的最佳运行时,然后将结果作为 Ruby 对象返回给您。


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

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

    #Gemfile
    gem 'execjs'       
    gem 'therubyracer' 
    

    ExecJS and could not find a JavaScript runtime

    【讨论】:

    • 这行得通,我已经接受了这个作为我重新创建此问题的答案,并且不必将我的咖啡脚本降级到 1.8。安装 node.js 解决了这个问题。
    【解决方案2】:

    你的销毁链接中是否有方法::delete,如下所示?

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

    你应该使用 application.js 而不是默认

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

    我假设您在使用 windows 上的咖啡脚本版本时遇到了同样的问题。请检查以下链接以更改和更新咖啡脚本 gem。

    Rails ExecJS::ProgramError in Pages#home?

    gem 'coffee-script-source', '1.8.0'

    捆绑更新咖啡脚本源

    【讨论】:

    • 抱歉,我刚刚将这段代码添加到问题的末尾,我确实有。
    • TypeError: Object does not support this property or method (in C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/turbolinks-2.5.3/lib/ assets/javascripts/turbolinks.js.coffee) 将“default”更改为“application”时出现此错误
    • @richpeck 这确实有效,我在评论,然后在每次评论后更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多