Rails指南:入门5.13删除文章:确认对话框WON' T出现

时间:2015-09-15 23:17:41

标签: javascript jquery ruby-on-rails ruby windows

我目前正试图让Rails在我的Windows 7机器上运行而且我遇到了困难。我是Rails的新手(通过我使用入门指南推断)并且我坚持这个:

http://guides.rubyonrails.org/getting_started.html#deleting-articles

具体来说,你猜对了,确认对话框。它永远不会出现,Rails只使用SHOW路线。

现在代码:

应用\资产\ Javascript角\的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 jquery.ui.all
//= require_tree .

应用\控制器\ articles_controller.rb

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

应用\视图\物品\ 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 %></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>

和app \ views \ layouts \ application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

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

现在在命令行中,当我点击&#34; link_to,&#34;销毁链接,我得到:

[2015-09-15 18:48:26] ERROR Errno::ECONNRESET: An existing connection was forcibly closed by the remote host. @ io_fillbuf - fd:10
    C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:80:in 'eof?'
    C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:80:in 'run'
    C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in 'block in start_thread'
  Rendered C:/...

请注意,在以下情况之后一切正常:

Rendered C:/

为了解决这个问题,我有:

  • 确保SQLite在我的机器上
  • 已安装SQLite gem
  • 为我的网络浏览器启用了活动脚本(Opera;基于 铬)

我已经完成了Stack Exchange和Stack Overflow的每一条建议,包括:

使用以下命令修改index.html.erb:

<%= link_to 'Destroy',  post,  method: :delete, data: { confirm: 'Are you sure?' } %>

或:

link_to 'Cancel?', schedule_path(current_user.schedules[0]), :confirm => "are you sure?", :method => :delete

或:

<td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>

或:

<td><%= link_to 'Destroy', [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' } %></td>
  • 确保因为我在Windows 7上,我的CoffeeScript就是 版本1.8.0和我更新了Gemfile
  • 确保宝石,&#34; jquery-rails,&#34;包含在Gemfile文件中
  • 使用方法:destroy而不是方法:delete
  • 我的佣金路线看起来应该如此。一个答案提到了确定 bin/rake routes返回正常的内容

确保application.js文件包含:

//= require jquery
//= require jquery_ujs

(上面你可以看到它确实如此)

什么都没有,没什么,已经有效了。

我尝试过的唯一让我 ANY 成功的就是将index.html.erb中的link_to替换为button_to

但这并没有产生确认窗口。它只是执行了DESTROY。

我很困惑,我花了很多时间试图让它发挥作用。

我从这些链接中找到了答案:

我可能会遗漏一些链接和答案,所以请随时将我链接到您认为我可能错过的帖子。

感谢您阅读 MASSIVE 文字墙,希望我能尽快找到解决问题的方法。

1 个答案:

答案 0 :(得分:1)

我不认为布局正在正确加载您的JavaScript。

试试这个。希望它有所帮助!

应用/视图/布局/ application.html.erb

<!DOCTYPE html>
<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>