views:

969

answers:

2

Hello

I am trying to create a link to destroy and entry in the DB using AJAX but I also want it to function without JavaScript enabled

However the following code

<%=link_to_remote "Delete", :update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>

produces the output

 <a href="#" onclick="new Ajax.Updater('section_phone', '/phone_numbers/destroy/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('b64efb643e49e9af5e2e195a90fd5a8b6b99ece2')}); return false;">Delete</a>

For some reason the url_for does not work properly and the href tag is set to #. I do not know why this does not work since I am not notified of any errors in the log

Does anyone know why this could be?

Thank you

+1  A: 

I believe your looking for something like this: RailsCasts - Destroy Without JavaScript

vrish88
Not exactly. The issue is not with the js confirmation but has to do with why url_for is not working.
rube_noob
Actually it is working properly. If you take a look at http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001613 you'll see that your link is the one that link_to_remote is producing. So in order to have it degrade properly, take a look at the screencast.
vrish88
+2  A: 

You're missing curly braces around the options{} hash, which :update and :url belong to, to separate them from the html_options{} hash that :href belongs to.

Try this:

<%=link_to_remote "Delete", {:update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }}, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>

That'll get the URL to show up as the href attribute of your link, but a GET request to your destroy action shouldn't delete it. You'll need something else (like what vrish88 suggests) so that you can make a GET request to the destroy action to get a form, then POST that form to actually delete the phone number.

Joe W.