1

I have several links I'm trying to create to either perform custom controller methods or the destroy action. I'm using jquery via the jquery-rails gem and have ran the generator and seems to be including properly:

<script type="text/javascript" src="/javascripts/jquery.js?1312255663">
<script type="text/javascript" src="/javascripts/rails.js?1312407526">
<script type="text/javascript" src="/javascripts/jquery.min.js?1312255664">
<script type="text/javascript" src="/javascripts/application.js?1312256996">

Here is an example of a link using :method => :delete:

<%= link_to "Delete", list_path(@list), :method => :delete, :confirm => "Are you sure?" %>

with my routes.rb:

resources :lists

I think maybe the issue might be with my controller methods, but I still don't understand why it would be rendering the show action, here's my destroy method for the above link/controller.

def destroy
    @list = current_user.lists.find(params[:id])
    @list.active = false
    @list.save

    if @list.save
      redirect_to root_path, :notice => "List '#{@list.name}' deleted."
    else
      render :action => 'edit'
    end
  end

Is there anything obvious that I'm missing here, specifically with the jquery as that's where a lot of people seem to have this same issue.

Kombo
  • 2,371
  • 3
  • 34
  • 64
  • 1
    I don't know if that's the issue but why are you including jquery twice? The `min` version is simply a compressed version of the same JS file. Try leaving out `jquery.min`. – M. Cypher Aug 03 '11 at 22:49
  • In my layout, I'm just including all the .js files with :all. I've moved it just for giggles, no change on functionality. Thanks for letting me know they were one in the same. – Kombo Aug 03 '11 at 22:52
  • Does it work if you don't include jquery at all? – M. Cypher Aug 03 '11 at 22:57
  • Nope, button_to is working though so I'll probably just go forward with that as it's safer like you suggested below. – Kombo Aug 03 '11 at 23:04
  • You are missing rails.js file. donwload it from https://github.com/rails/jquery-ujs/raw/master/src/rails.js and put it into your javascripts folder. Should work. – Yunwei.W Dec 13 '12 at 15:46

4 Answers4

4

I'm not sure whether or not the issue is related to jquery, but I would suggest replacing link_to with button_to. link_to should work too, but button_to is "the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators" (source: Rails API doc)

M. Cypher
  • 6,966
  • 2
  • 34
  • 34
  • There's not really any need for `button_to` in terms of search bots — they don't execute JS and that's how DELETE links are handled (on-click, JS creates a form with a hidden `_method=delete` field and a POST action and submits it). – coreyward Aug 03 '11 at 23:12
  • Are you sure? If I write `<%= link_to 'Test', '/', :method => :delete %>`, Rails will turn that into `Test`. A search bot might see that as an ordinary link and follow it, no? (Yes, it's nofollow, but that's not always respected) – M. Cypher Aug 03 '11 at 23:17
  • It could follow it, and it'll just be a GET method request. The `data-method` attribute is just following the HTML spec for adding custom attributes to elements — it isn't given any weight/functionality by browsers or bots (well, not generally anyway; someone could start paying attention to it theoretically). – coreyward Aug 03 '11 at 23:30
2

If you could put what you have done and what is/isn't happening in your question that would help greatly in diagnosing what is happening.

Some diagnostic steps for you:

  1. Verify that the file at /javascripts/rails.js is loading what you're expecting (check in the browser).
  2. Ensure that you're getting the expected confirmation dialog when you click on the delete link.
  3. Check your log file (tail -f your_app/logs/development.log); make sure you're seeing a DELETE request to the appropriate path and it is routing to the appropriate controller action.
  4. Remove the first call to @list.save, leaving only the one as part of the conditional.
  5. Check to see if the updated_at timestamp on your list object is being changed.
coreyward
  • 77,547
  • 20
  • 137
  • 166
0

Reference the use of rails.js: RAILS.JS generated with new Rails app and contains all the unobtrusive handlers. But, it relies on PROTOTYPE, so if you want to rely on jQuery, use the following instead!! CAN'T USE BOTH BECAUSE THEY CONFLICT!! Ref: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3 = USE javascript_include_tag 'jquery_ujs.js' DON'T USE: /= javascript_include_tag 'rails.js' /= javascript_include_tag 'jrails.js' "officially obsolete" Of course with asset pipeline you can just use your main application.js file and reference all the others javascript_include_tag 'applications.js'

Tom Bartel
  • 56
  • 5
0

I think that the answer to your question is that jQuery messes the :method => :delete in your link_to. As you probably know, if you would not specify :method => :delete, then according to REST, you would be redirected into show action. My bet ( 99,9% ) that this is what happens. Reason: there is no such thing as request DELETE, there are only GET and POST. Rails do a trick to make it delete ( I guess by inserting _method = delete if i remember correctly )

mkk
  • 7,583
  • 7
  • 46
  • 62