Rails, default.js is empty, and jquery and jquery_

2019-06-08 14:11发布

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.

1条回答
Melony?
2楼-- · 2019-06-08 14:56

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

查看更多
登录 后发表回答