I am working through the Getting Started tutorial (creating a Blog) and the link_to Destroy is not functioning properly. In the terminal it always interprets it as #SHOW. <
Make sure gem 'jquery-rails' is in gemfile and jquery_ujs is included in app/assets/javascripts/application.js file
//= require jquery
//= require jquery_ujs
Source: https://github.com/rails/jquery-ujs
I got the same problem. I work out by this way:
My env. is: A. ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32] B. Rails 4.2.5 C. Browser: Firfox 44.02
add include js in html page because delete function will use js to handle.
<%= javascript_include_tag "application" %>
add skip_before_action :verify_authenticity_token
to controller for prevent InvalidAuthenticityToken in AdsController#destroy
add link_to into your web page
<%= link_to 'Delete', post, method: :delete, data: {confirm: "Are you sure?"}%>
Now, I can use link_to to delete a record from my page.
Make sure that you have a Delete REST method via rake routes.
DELETE /articles/:id(.:format) articles#destroy
I've got the same problem only with this blog version as I'm doing both. http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/
I'm also trying to learn how to use the web console and pry within it. My problem is that binding.pry doesn't show up in destroy method but will in show. That tells me it must be a bad link right? It's not getting to the destroy method even. Thank you all for your answers. We do have to try things don't we? Trying
<td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
is not going to work for that one.
Here's what he has
<td><%= link_to 'Destroy', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete %></td>
because you only want to delete the comment here.
But combining things so far like the following gives no errors but I still have no binding.pry from the destroy method in the controller.
<td><%= link_to 'Destroy', [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' } %></td>
and I never get the confirmation like you would expect.
So do try to figure out what's going on with the jquery as suspect. I can confirm that was my case but the rest I'll leave for nubes because this will show up in a google search.
I had the same issue and realized that I had created my rails app with -J
which is the same as --skip-javascript
.
You need JavaScript enabled for this to work; the lack of a popup to confirm the delete is a dead giveaway.
I had exactly this problem and it was caused by triple-clicking the Guide's code block and copy/pasting the code straight into my editor (ST2 on OSX).
You should check that the generated HTML for the link looks like this:
<a data-confirm="Are you sure?" data-method="delete" href="/posts/3/comments/3" rel="nofollow">Destroy Comment</a>
If it doesn't, but instead looks like the below, then the Javascript won't be applied:
<a href="/posts/3/comments/3" data="{:confirm=>"Are you sure?"}" method="delete">Destroy Comment</a>
The large gaps between the href
and the unparsed Ruby are caused by the non-breaking spaces (unicode character \u00a0
) used in the Guide being copied into your script.
I'm not sure why this doesn't cause a script parse error.
Using the button_to
method instead of link_to
seemed to work, minus the fact that it was not confirming(which I felt was important).
In my case, I was using react-on-rails and had multiple layouts.
Adding <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
to my hello-world layout fixed the problem.