我很抱歉问了一个可能是补救性的问题,但在学习 Rails 时,我试图按照本教程中的注释进行注释:
http://guides.rubyonrails.org/getting_started.html
昨晚我在本教程中发布了一个类似的问题,并得到了迅速的回复,这对我帮助很大,所以我也希望如此。提前谢谢你。
第 5.14 节:删除帖子
我被指示添加一个删除链接到 index.html.erb 页面
<h1>Listing Posts</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post_path %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
生成:
<td><a data-confirm="Are you sure?" data-method="delete" href="/posts/1" rel="nofollow">Destroy</a></td>
它看起来不错,但是当我点击链接时,我既没有得到确认也没有被引导到删除操作。它生成此 HTTP 操作
Request URL:http://localhost:3000/posts/1
Request Method:GET
Status Code:200 OK
Rails:4,Ruby:2,64 位 Windows 8,chrome:版本 28.0.1500.72 m
再次感谢
添加 application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Listing</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag :application %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
产生了这个错误:
ExecJS::RuntimeError in Posts#index Showing C:/Ruby-Projects/listing/app/views/layouts/application.html.erb where line #6 raised:
(in C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/turbolinks-1.3.0/lib/assets/javascripts/turbolinks.js.coffee) Extracted source (around line #6): 3 4 5 6 7 8 9 Listing <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag :application %> <%= csrf_meta_tags %>
Rails.root: C:/Ruby-Projects/listing
Application Trace | Framework Trace | Full Trace app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___567964991_28724900' Request
Parameters:
None
应用程序.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
最佳答案
确保你有
//= require jquery
//= require jquery_ujs
进入application.js文件,application.js文件包含在view/layout/application.html.erb
文件
关于ruby-on-rails - Rails link_to 方法 : :delete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748329/