thank_you
thank_you

Reputation: 11107

ajax link not working though jquery is loaded

I have a link to in my view

= link_to "Remove from Handy List", {controller: "handy_lists", action: "destroy_via_ajax", tradie_id: "1"}, method: :get, remote: true, class: "pull-right"

In my routes I have

get "handy_lists/destroy_via_ajax/:tradie_id", to: "handy_lists#destroy_via_ajax"

The action for now simply displays

def destroy_via_ajax
  puts "it deletes"
end 

Whenever I click the button the server isn't responding. I've checked and javascript and jquery are loaded so I know it can't be that. I don't know what I'm missing. What's wrong with the link and how can I fix it?

Upvotes: 1

Views: 185

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

There are several things which could be wrong:

Is your remote link working?

This would be the first thing to test. I would use the development console in either Chrome or Mozilla & click on the "network" tab, to see if the link is actually requesting some sort of ajax link.

Likelihood is that it is, but I don't think you've set up your system correctly


Your controller setup could be incorrect

You're calling the action "destroy with ajax"... why not just use the respond_to function in the normal destroy action?

You could try this:

def destroy
    respond_to do |format|
        format.js
    end
end

This will allow you to create destroy.js.erb in your views/controller folder, where you can then call something like:

alert('Deleted Successfully!');

This should work, but I am open to chat if you need more help!

Upvotes: 1

Related Questions