简体   繁体   中英

Rails 4.1.8 link_to Delete, JavaScript not being applied

I'm following the Getting started with Rails guide.

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

I'm having an issue when trying to use the link_to 'Delete' method to destroy an article. The server console shows that the DELETE is not being used and instead the GET method is being called instead, thus not following the route. In section 5.13 of the guide...

articles_controller.rb

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

    redirect_to articles_path
  end

There are many similar questions with regards to this topic, yet I have not found the solution as to why this doesn't work. (I know button_to does work but there must be a reason why the guide refers to link_to)

I have checked my application.js file and ensured that jquery and jquery-ujs are included:

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

Console output:

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)

The layout file application.html.erb

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

Using browser developer tools, it seems that the JS file is included correctly. I have also checked in different browsers and ensured that javascript is running on the browsers.

Here is the browser <head> section:

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

I've tried changing the application.js file to the following as suggested 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>

Get the following error: 在此处输入图片说明

Is there anything else I can try to get link_to working as intended? I know that rails uses javascript for the link_to DELETE and the confirmation box and it's not being displayed.

edit: added view file where link_to is being used.

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>

do you have method: :delete in your destroy link like following?

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

you should use application.js instead of default

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

I assume you are having the same problem with coffee script version on windows. please check following link to change and update coffee script gem.

Rails ExecJS::ProgramError in Pages#home?

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

bundle update coffee-script-source

The error seems to be that ExecJS is not properly configured for your system:

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.


If you're using Windows, you should download & install nodeJS

If you're using Linux / Mac, you should add therubyracer / execjs to your Gemfile :

#Gemfile
gem 'execjs'       
gem 'therubyracer' 

ExecJS and could not find a JavaScript runtime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM