Rails指南:入门5.13删除文章:不会出现“确认"对话框 [英] Rails Guides: Getting Started 5.13 Deleting Articles: Confirmation Dialog Box WON'T Appear
问题描述
我目前正在尝试让Rails在Windows 7机器上运行,但遇到了困难.我对Rails还是很陌生(根据我对《入门指南》的使用得出的结论),我对此深信不疑:
I am currently trying to get Rails to run on my Windows 7 machine and I am having difficulty. I am very new to Rails (inferred by my use of the Getting Started guide) and I am stuck on this:
http://guides.rubyonrails.org/getting_started.html#deleting-articles
您猜对了,特别是确认对话框.它永远不会出现,Rails只是利用SHOW路线.
Specifically the, you guessed it, confirmation dialog box. It never appears and Rails just utilizes a SHOW route.
现在输入代码:
app \ assets \ javascripts \ application.js
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
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
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
and 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"销毁链接时,我得到:
Now in the command line, when I click the, "link_to," Destroy link, I get:
[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:/...
请注意,在执行以下操作后,一切似乎都很正常
Note that everything seems normal after:
Rendered C:/
要我自己解决此问题,我有:
To fix this issue on my own, I have:
- 确保SQLite在我的计算机上
- 已安装SQLite gem
- 已为我的Web浏览器(Opera;基于 铬)
- Made sure SQLite is on my machine
- The SQLite gem is installed
- Active scripts are turned on for my web browser (Opera; based on Chromium)
我已经遵循了Stack Exchange和Stack Overflow的所有建议,包括:
I have followed every piece of advice I could from Stack Exchange and Stack Overflow, including:
使用以下方法修改index.html.erb:
Modifying index.html.erb with:
<%= 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
- 确保Gemfile文件中包含宝石"jquery-rails"
- 使用方法:销毁而不是方法:删除
- 我的耙路看上去像应该的那样.一个答案提到要确保
bin/rake routes
返回正常结果,并且确实如此 - Making sure that because I am on Windows 7, that my CoffeeScript is version 1.8.0 and that I updated the Gemfile as such
- Making sure the gem, "jquery-rails," is included in the Gemfile file
- Using method: destroy instead of method: delete
- My rake routes look as they should. One answer mentioned making sure
bin/rake routes
returned something normal and it does
确保application.js文件包含:
Making sure the application.js file includes:
//= require jquery
//= require jquery_ujs
(您可以在上面看到)
没有任何效果,没有任何效果.
我尝试给我带来的任何成功的唯一方法就是用button_to
替换index.html.erb中的link_to
.
The only thing I have tried that has given me ANY success is replacing the link_to
in index.html.erb with button_to
.
但这不会产生确认窗口.它只是执行销毁操作.
But this doesn't produce a confirmation window. It just performs a DESTROY.
我很困惑,我花了很多时间试图使它生效.
I am very confused and I have spent many hours trying to get this to work.
我从这些链接中找到了答案:
I have found my answers from these links:
- >铁路CRUD-销毁路径始终为铁路到用户显示页面
- Rails-4,ExecJS :: ProgramError in Pages#welcome
- Rails,default.js为空,并且未加载jquery和jquery_ujs
- 路轨4 link_to销毁不在入门教程
- Rails CRUD - Destroy path always routes to the User show page
- Rails-4, ExecJS::ProgramError in Pages#welcome
- Rails, default.js is empty, and jquery and jquery_ujs not loaded
- Rails 4 link_to Destroy not working in Getting Started tutorial
我可能缺少一些链接和答案,所以请随时将我链接到您认为我可能错过的话题.
I may be missing a few links and answers, so please feel free to link me to a thread you think I may have missed.
感谢您阅读这本大量的文字墙,希望我能尽快找到解决问题的方法.
Thank you for reading this MASSIVE wall of text and I hope I can find a solution to my problem soon.
推荐答案
我认为布局无法正确加载您的JavaScript.
I don't think the layout is loading your javascript properly.
尝试一下.希望对您有帮助!
Try this. Hope it helps!
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>
这篇关于Rails指南:入门5.13删除文章:不会出现“确认"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!