簡體   English   中英

Rails link_to方法:: delete

[英]Rails link_to method: :delete

我很抱歉詢問可能是一個補救問題,但是在學習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>

產生了這個錯誤:

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

應用程序跟蹤| 框架跟蹤| 完整跟蹤應用/視圖/布局/ application.html.erb:6:在`_app_views_layouts_application_html_erb___567964991_28724900'請求

參數:

沒有


的application.js

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

確保你有

//= require jquery
//= require jquery_ujs

到application.js文件和application.js文件包含在view/layout/application.html.erb文件中

好的,我需要從application.js中刪除這兩行

//= require turbolinks
//= require_tree .

擺脫了ExecJS運行時錯誤。 為什么默認包括這些,我應該首先弄清楚它們為什么會導致錯誤?

FWIW - 刪除功能現在可以使用。


這篇文章的道具:

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

我的產品應用程序有這個列出的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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM