Rails link_to方法:: delete

时间:2013-07-19 14:15:45

标签: ruby-on-rails html5

我很抱歉询问可能是一个补救问题,但是在学习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 #index显示   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                       清单       &lt;%= stylesheet_link_tag“application”,media:“all”,“data-turbolinks-track”=&gt; true%&gt;       &lt;%= javascript_include_tag:application%&gt;       &lt;%= csrf_meta_tags%&gt;

     

Rails.root:C:/ Ruby-Projects / listing

     

应用程序跟踪|框架跟踪|完整追踪   应用程序/视图/布局/ application.html.erb:6:在   `_app_views_layouts_application_html_erb___567964991_28724900'请求

     

参数:

     


的application.js

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

3 个答案:

答案 0 :(得分:11)

确保你有

//= require jquery
//= require jquery_ujs

进入application.js文件,application.js文件包含在view/layout/application.html.erb文件

答案 1 :(得分:5)

好的我需要从application.js

中删除这两行
//= require turbolinks
//= require_tree .

摆脱了ExecJS运行时错误。为什么默认包括这些,我应该首先弄清楚它们为什么会导致错误?

FWIW - 删除功能现在有效。


支持这篇文章:

https://stackoverflow.com/questions/12520456/execjsruntimeerror-on-windows-7-trying-to-follow-rubytutorial

答案 2 :(得分:2)

我的产品应用程序有这个列出的js文件。

请你查看资产文件夹中的所有文件。

自动加载你的帖子控制器。

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