开发者

Rails 3 - link_to :delete is redirecting to show action with jquery installed

I have several links I'm trying to create to either perform custom controller methods or the de开发者_StackOverflowstroy 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.


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)


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.


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 )


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'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜