Html Rails link_to 方法: :delete

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17748329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:19:57  来源:igfitidea点击:

Rails link_to method: :delete

ruby-on-railshtml

提问by akaphenom

I am sorry for asking what may be a remedial question, but in learning rails i was trying to follow along note for note in this tutorial:

我很抱歉问什么可能是一个补救问题,但是在学习 Rails 时,我试图按照本教程中的注释进行操作:

http://guides.rubyonrails.org/getting_started.html

http://guides.rubyonrails.org/getting_started.html

I posted a similar question from this tutorial last night and got a prompt response which helped me significantly, so I am hoping for the same. Thank you in advance.

昨晚我从本教程中发布了一个类似的问题,并得到了及时的回复,这对我有很大帮助,所以我希望也是如此。先感谢您。

Section 5.14: Deleting Posts

第 5.14 节:删除帖子

I am instructed to add a delete link to the index.html.erb page

我被指示向 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>

which generates:

它产生:

<td><a data-confirm="Are you sure?" data-method="delete" href="/posts/1" rel="nofollow">Destroy</a></td>

It looks OK to me, but when I click on the link I get neither the confirmation nor am I directed to the delete action. It generates this HTTP action

对我来说看起来没问题,但是当我点击链接时,我既没有得到确认,也没有被定向到删除操作。它生成此 HTTP 操作

Request URL:http://localhost:3000/posts/1 
Request Method:GET 
Status Code:200 OK 

Rails: 4, Ruby: 2, 64 bit windows 8, chrome: Version 28.0.1500.72 m

Rails:4,Ruby:2,64 位 windows 8,chrome:版本 28.0.1500.72 m

Thank you again

再次感谢你



Adding the application.html.erb

添加 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>

which yielded this error:

产生了这个错误:

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

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 列表 <%= 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

应用程序.js

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

采纳答案by akaphenom

OK i needed to remove these two lines from application.js

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

//= require turbolinks
//= require_tree .

which got rid of the ExecJS run time error. Why were these included by default, and should I be figuring out why they were causing an error in the first place?

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

FWIW - the delete functionality now works.

FWIW - 删除功能现在可以使用。



Props to this post:

这篇文章的道具:

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

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

回答by benchwarmer

Make sure you have

确保你有

//= require jquery
//= require jquery_ujs

into application.js file and application.js file is included into view/layout/application.html.erbfile

进入 application.js 文件和 application.js 文件被包含到view/layout/application.html.erb文件中

回答by supercool

my product application have this listed js files.

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

would you please check wheather you have all the files in assets folder.

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

which autoloads your post controller called.

它会自动加载您调用的后期控制器。

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

now see my index.erb.html files

现在查看我的 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 %>