Kombo Kombo - 1 year ago 109
jQuery Question

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

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

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.

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download