Rails的link_to方法:删除

8
对不起,我问一个可能是基础问题,但在学习 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 Version 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>

这导致了以下错误:

Posts#index中的ExecJS::RuntimeError,显示在C:/Ruby-Projects/listing/app/views/layouts/application.html.erb,其中 第6行引发了错误:

(在 C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/turbolinks-1.3.0/lib/assets/javascripts/turbolinks.js.coffee) 摘录(大约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

应用跟踪 | 框架跟踪 | 完整跟踪 app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___567964991_28724900' 请求

参数:


application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

在你的布局中有没有<%= javascript_include_tag :applicaton %> - Marek Lipka
当我添加它时,它会给我一个错误。 - akaphenom
请确保您的 JavaScript 代码正确无误。 - Marek Lipka
3个回答

11

确保你已经

//= require jquery
//= require jquery_ujs

将代码添加到application.js文件中,而application.js文件被包含在view/layout/application.html.erb文件中。


我觉得我已经将它包含在application.js中了,我该如何确保通过application.erb包含了application.js? - akaphenom
将此行 <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 添加到 application.html.erb 文件中。 - benchwarmer

5

好的,我需要从application.js中删除这两行代码。

//= require turbolinks
//= require_tree .

这个问题已经解决了ExecJS运行时错误。默认情况下为什么会包含这些内容,我应该先找出它们为什么会导致错误吗?

顺便说一下 - 删除功能现在可以使用。


感谢这篇文章:

https://dev59.com/Wmcs5IYBdhLWcg3w3HoG


2

我的产品应用程序有以下列出的js文件。

请检查您是否在资产文件夹中拥有所有文件。

其中自动加载了名为post controller的控制器。

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/products.js?body=1" type="text/javascript"></script>
<script src="/assets/say.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

现在请查看我的index.erb.html文件

<h1>Listing products</h1>

<table>
<tr>
<th>title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @products.each do |product| %>
<tr class="<%= cycle('list_line_odd','list_line_even') %>">
<td><%= product. title %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' }
%></td>
</tr>
<% end %>
</table>

<br/>

<%= link_to 'New Product', new_product_path %>

嘿@supercool,我完全是意外地给这个点了踩!! 直到现在我才意识到,显然在进行编辑之前我无法取消我的踩....不想伤害你的声誉,所以也许你可以进行最小的编辑(添加一个随机空格等),这样我就可以清除我的踩。 - vosmith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接