1

I am making a Ruby on Rails app and have run into a problem which I believe has to do with the Javascript in my app not working.

The specific issue I'm having is with a link_to:

<%= link_to 'Sign out', destroy_user_session_path, :method => :delete %>

Fairly standard as you can see.

I am getting this in my console:

Started GET "/user/sign_out" for ::1 at 2015-06-02

I think this may have something to do with bootstrap and my application.js file but any solution I have found online have no helped.

Here is my application.js file:

//= require bootstrap-sprockets
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Earlier in my app I was having a similar issue with deleting my "space" entities within the same app, and other similar JS issue so it must be that.

This is on Rails 4 by the way

user3342292
  • 139
  • 5

2 Answers2

0

The Rails Unobtrusive Javascript Driver (UJS) - jquery-rails by default uses the intercepts clicks on links with data-method and sends an AJAX request which either sends the correct HTTP method or fakes it when the client does not support a particular verb by sending a POST request with a special _method parameter.

This is due to the fact that HTML only supports GET actions for links and GET|POST for forms.

You most likely have an issue with your javascript such as a syntax error which is preventing the UJS driver from doing its job. Open up the console in your browser.

Community
  • 1
  • 1
max
  • 76,662
  • 13
  • 84
  • 137
  • Also see http://ryantownsend.co.uk/post/13058102942/unobtrusive-object-deletion-in-rails – max Jun 02 '15 at 18:02
  • I'm using the devise gem so I do not have a sessions controller or resource and thus cannot delete it like that. Opening the console doesnt accomplish much as nothing is shown no matter what I click on the page – user3342292 Jun 02 '15 at 19:37
  • Open the network tab in the developer tools - if the UJS driver is working you should see a POST request when you click the delete link. Bootstrap depends on jQuery so it should be listed after jQuery in your application.js. But if it was loading out of order you should have seen an `Uncaught ReferenceError` in the console. – max Jun 03 '15 at 00:16
  • @user3342292 actually, you **do** have `SessionsController` from Devise, it's just not stored in your application by default. `rake routes` are well able to show its routes. And your link code appears to be correct to me, it's the UJS that doesn't work properly. You must have hit the wrong answer :) – D-side Jun 03 '15 at 09:30
0

Check the whitespaces.

I checked all the comments here. All the includes were set correctly. But I noticed that deleting articles did not work, while deleting comments would work. After rewriting some code and checking here and there, I found two files that looked identically in the editor, but one version would work and one would not work!

curry-blog/app/views/articles/index.html.erb:

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Delete', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>        
    </tr>
  <% end %>
</table>

However, looking at the files with xxdiff I found that in one version only tabs where used, while the other also used blanks. Probably, from copy pasting code from the tutorial. Replacing the blanks with tabs did therefore fix the problem.

Sören
  • 4,572
  • 9
  • 36
  • 74