1

I am practicing ruby on rails by using rails guides. In the section 5.13 Deleting Articles they are showing how to create the delete (destroy) functionality. I followed the steps correctly, but the delete confirmation dialogue isn't shown and the articles are not getting deleted. When I checked the chrome developer tools, "jquery" and "jquery_ujs" is not included and the "default.js" is empty. I am running ruby on rails on windows 7.

This is my articles controller,

class ArticlesController < ApplicationController

  def index
    @articles = Article.all
  end

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

  def new
    @article = Article.new
  end

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

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

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

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

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

    redirect_to articles_path
  end

  private
  def article_params
    params.require(:article).permit(:title, :text)
  end
end

And this is view (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 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>

And this part of the tutorial is used creating this delete/destroy functionality.

Edit: I didn't edit any js file, because the said tutorial didn't say to alter any js file at this point, but as someone asked in the comment so I'm adding the js file, this is how the application.js looks.

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

Edit 2: (edited after @NitishParkar's comments)
As I was following the Rails Guide, when I reached to this point, where the CRUD portion is completed for the articles controller. This is how the listed articles and the links to "show", "edit" and "destory" looked.

Listing Articles

When I inspected the source for the first destroy(delete) link, it looked like this: <a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/articles/1">Destroy</a>

But when I clicked it, it didn't show any confirmation dialog box and the browser jumped to http://localhost:3000/articles/1 and the article wasn't deleted.

Cute Tom
  • 111
  • 3
  • 9
  • In your layout, which js are you loading using `javascript_include_tag`? Post the content of that js file. – Nitish Parkar Jun 15 '15 at 17:49
  • Hi @NitishParkar, thank you for the comment, I didn't play with the js file, because I am following the said tutorial, and it didn't say to alter any js file, but I'll include the application.js in the question. – Cute Tom Jun 15 '15 at 20:20
  • Hi @NitishParkar are you there? – Cute Tom Jun 19 '15 at 22:55
  • Everything looks fine. When you click on the destroy link, does it take you to another page? – Nitish Parkar Jun 20 '15 at 05:28
  • @NitishParkar Let me edit the question again, including the page I land on and the link structure etc. – Cute Tom Jun 23 '15 at 12:39
  • Are you loading `application.js` using `javascript_include_tag` in your layout file? – Nitish Parkar Jun 23 '15 at 14:07
  • Actually I am a newbie to the ruby world, I'm a php developer basically, and including application.js manually isn't mentioned in rails guides : getting started. Should I include it manually? – Cute Tom Jun 23 '15 at 16:09
  • 1
    Thank You so much NitishParkar for being engaged with me, the answer form @Bangash worked for me. :) – Cute Tom Jun 23 '15 at 17:02

1 Answers1

1

Everything is OK from your side. You said that you're on Windows 7, I also experienced this problem once when I needed to run rails on windows for some reason. Than I found an answer here which helped me to get out of this problem. Actually coffee-script-source, v 1.9.x gives problem on windows, so if you use v 1.8.0 instead, this problem will go away.

include in your Gemfile gem 'coffee-script-source', '1.8.0'

than run bundle update coffee-script-source

Community
  • 1
  • 1
Bangash
  • 1,152
  • 3
  • 10
  • 30