【问题标题】:Rails Guides: Getting Started 5.13 Deleting Articles: Confirmation Dialog Box WON'T AppearRails 指南:入门 5.13 删除文章:确认对话框不会出现
【发布时间】:2015-09-15 23:17:41
【问题描述】:

我目前正在尝试让 Rails 在我的 Windows 7 机器上运行,但遇到了困难。我对 Rails 很陌生(通过使用入门指南推断),我被困在这个问题上:

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

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

现在是代码:

app\assets\javascripts\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 .

app\controllers\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

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

现在在命令行中,当我单击“link_to”时,销毁链接,我得到:

[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
  • 确保 gem“jquery-rails”包含在 Gemfile 文件中
  • 使用方法:destroy 而不是方法:delete
  • 我的 rake 路线看起来应该是这样。一个答案提到确保 bin/rake routes 返回了一些正常的东西,它确实如此

确保 application.js 文件包括:

//= require jquery
//= require jquery_ujs

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

没有任何效果,NOTHING

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

但这不会产生确认窗口。它只是执行 DESTROY。

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

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

我可能遗漏了一些链接和答案,因此请随时将我链接到您认为我可能遗漏的主题。

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

【问题讨论】:

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


    【解决方案1】:

    我认为布局没有正确加载您的 javascript。

    试试这个。希望对您有所帮助!

    app/views/layouts/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>
    

    【讨论】:

    • 使用你给我的 application.html.erb 给了我另一个错误:Articles#index 中的 Sprockets::FileNotFound,这导致我进入这个页面:*.com/questions/30239617/… 并通过删除 //= require来自我的 application.js 的 highcharts,它工作正常。非常感谢你的帮助! DESTROY 现在可以正常工作,并且 Javascript 弹出窗口也可以正常工作。谢谢。
    • 我正在运行 rails 版本。 4.2.5.1,红宝石版本。 2.3.0p0,在 CentOS 6.6 Linux 机器上,我有完全同样的问题。根据“Ruby on Rails 入门”指南,我无法让 Destroy 对话框正常工作。这非常烦人。这是一个常见的问题,有很多帖子,很多解决方案。我都试过了。 “销毁”使用“link_to”动词。如果更改为“button_to”,则它可以工作,但没有对话框。我的“application.html.erb”文件与此处显示的相同,仍然没有“删除”方法。这不是 Windows/浏览器问题,例如 iPad 的相同行为。
    最近更新 更多