1

I have a problem very similar to this one: rails 3 - link_to to destroy not working

But delete/destroy links do not work; I simply get redirected to the show page of the object. When I make a button for delete, it all works fine. But I'd like to understand why. Does anyone know?

I seems to be related to some .js files I am using/calling.

<!-- This link doesn't work -->
<%= link_to('Delete', post, :confirm => 'Are you sure?', :method => :delete) %>-->

<!-- This button does work -->
<%= button_to "delete", post, :method=>:delete, :class=>:destroy, :confirm => 'Are you sure?' %>

Post Controller

def destroy
  @post = Post.find(params[:id])
  @post.destroy

respond_to do |format|
  format.html { redirect_to(posts_url) }
  format.xml  { head :ok }
end

end

UPDATE After doing some further research it seem that everyone else having a similiar issue has included the following jquery library:

<script type="text/javascript" src="/javascripts/dragdrop.js"></script>

But I still don't know what the issue is...

LOG

Started GET "/posts/7" for 127.0.0.1 at Tue Jul 12 08:34:06 -0400 2011
Processing by PostsController#show as HTML
Parameters: {"id"=>"7"}
Post Load (0.2ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = 7 LIMIT 1
SQL (0.2ms)  SELECT COUNT(*) FROM "comments" WHERE ("comments".post_id = 7)
Rendered posts/show.html.erb within layouts/application (116.5ms)

HTML generated

<a href="/posts/17" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>

UPDATE: I've found that removing <script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script> fixes my problem. But I don't understand why. Is there a known conflict between jquery1.5.1 and rails 3.0.7?

Community
  • 1
  • 1
Tony Beninate
  • 1,926
  • 1
  • 24
  • 44
  • Could you please provide the first two lines of your log-file for each request? (the ones showing `Processing` and `Parameters`). Also the generated HTML output could be of interest. – arnep Jul 12 '11 at 11:56
  • Is what I posted above what you meant by the "log-file"? Sorry, I'm very new to rails... – Tony Beninate Jul 12 '11 at 12:37
  • Ya, this is the log I needed. Seems that the link does not send `DELETE` but `GET` so the action `show` is called instead of `destroy`. Please also provide the generated HTML source for the link. – arnep Jul 12 '11 at 12:48
  • Ok, I just posted the HTML generated above. Seems kinda odd that it the href would be to "/posts/17"... – Tony Beninate Jul 12 '11 at 21:26
  • I've also just found that removing the following script from my application.html.erb file fixes everything: – Tony Beninate Jul 13 '11 at 00:09
  • Seems that something with unobstrusive javascript translation does not work with this version of jquery. You can use the functionality of your browser right click on that link -> "inspect element" to see what is actually done to the DOM by jquery. – arnep Jul 14 '11 at 08:27

3 Answers3

3

Make sure you include these in your application layout:

<%= javascript_include_tag(:defaults) %>
<%= csrf_meta_tag %>
thenengah
  • 42,557
  • 33
  • 113
  • 157
1

In my case adding

<%= javascript_include_tag(:defaults) %>

did not work. However explicitly defining java script files did the trick

<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'application' %>

Not sure yet why :defaults tag didn't work...

alikk
  • 503
  • 6
  • 11
0

Rails will automatically load jquery for you if you have jquery-rails gem loaded via your Gemfile (this is the default rails configuration). It is then loaded via:

<%= javascript_include_tag(:defaults) %>

Have a look at app/assets/javascripts/application.js for the code that tells rails to add jquery via assets:

//= require jquery
//= require jquery_ujs

By trying to load another copy of jquery via this line:

<script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script>

You have caused a clash of jquery instances, the effect of which will be that neither will work.

ReggieB
  • 8,100
  • 3
  • 38
  • 46