Rails link_to method: :delete

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

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:

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

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

<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

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

Thank you again


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


application.js

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

回答1:

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



回答2:

OK i needed to remove these two lines from 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?

FWIW - the delete functionality now works.


Props to this post:

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



回答3:

my product application have this listed js files.

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!