【问题标题】:Rails, default.js is empty, and jquery and jquery_ujs not loadedRails,default.js 为空,未加载 jquery 和 jquery_ujs
【发布时间】:2015-08-31 05:56:00
【问题描述】:

我正在使用 rails guides 在 Rails 上练习 ruby​​。在5.13 Deleting Articles 部分中,他们展示了如何创建删除(销毁)功能。我正确地遵循了这些步骤,但没有显示删除确认对话框,并且文章没有被删除。 当我检查 chrome 开发人员工具时,“jquery”和“jquery_ujs”不包括在内,“default.js”为空。 我正在 Windows 7 上的 Rails 上运行 ruby​​。

这是我的文章控制器,

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

这是视图 (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>

this part of the tutorial 用于创建此删除/销毁功能。

编辑:我没有编辑任何 js 文件,因为上述教程没有说此时要更改任何 js 文件,但是正如评论中有人问的那样,我要添加js 文件,这就是 application.js 的外观。

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

编辑 2:(在 @NitishParkar 的 cmets 之后编辑)
当我遵循 Rails 指南时,当我到达 this point 时,文章控制器的 CRUD 部分已完成。这就是列出的文章以及指向“显示”、“编辑”和“破坏”的链接的外观。

当我检查第一个销毁(删除)链接的来源时,它看起来像这样:&lt;a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/articles/1"&gt;Destroy&lt;/a&gt;

但是当我点击它时,它并没有显示任何确认对话框,浏览器跳转到http://localhost:3000/articles/1,并且文章没有被删除。

【问题讨论】:

  • 在您的布局中,您使用javascript_include_tag 加载哪个js?发布那个js文件的内容。
  • 嗨@NitishParkar,谢谢你的评论,我没有玩过js文件,因为我正在按照上述教程进行操作,并且没有说要更改任何js文件,但是我'将在问题中包含 application.js。
  • 嗨@NitishParkar 你在吗?
  • 一切看起来都很好。当你点击破坏链接时,它会带你到另一个页面吗?
  • 非常感谢 NitishParkar 与我合作,@Bangash 的答案表格对我有用。 :)

标签: jquery ruby-on-rails ruby ruby-on-rails-4 rubygems


【解决方案1】:

在您这边一切正常。您说您使用的是 Windows 7,当我出于某种原因需要在 Windows 上运行 rails 时,我也遇到过这个问题。比我找到的答案here 帮助我摆脱了这个问题。实际上coffee-script-source,v 1.9.x 在windows 上会出现问题,所以如果你改用v 1.8.0,这个问题就会消失。

在您的 Gemfile 中包含 gem 'coffee-script-source', '1.8.0'

比运行bundle update coffee-script-source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多