【发布时间】: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。
我很困惑,我花了很多时间试图让它发挥作用。
我从这些链接中找到了答案:
- 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
我可能遗漏了一些链接和答案,因此请随时将我链接到您认为我可能遗漏的主题。
感谢您阅读此 MASSIVE 文字墙,希望我能尽快找到解决问题的方法。
【问题讨论】:
标签: javascript jquery ruby-on-rails ruby windows