user3254621
user3254621

Reputation: 29

Jquery AJAX call not finding Rails route

I'm working on a project where I need to be able to mark certain objects for review or deletion. I'm using checkboxes, then using JavaScript to harvest the data from the checkboxes. I'm trying to use AJAX to send that data back to the Rails Controller but I keep getting a 404 error, and I'm not sure what I'm doing wrong.

This is the AJAX call (review_list and purge_list are both defined, I've checked):

function callHandleSelected() {
...
  $.post('itemresults/handle_selected', { review: review_list, purge: purge_list },
      function(data) {
          alert(data);
      });
}

And this is the route I wrote to match it:

post 'itemresults/handle_selected', to: 'processed_item#handle_selected'

I've tried adding as: :ajax into the route to see if that makes a difference without any luck.

The HTML element that calls the ajax function looks like so:

<button type="button" class="btn btn-normal" onclick="callHandleSelected()">Mark Selected as Reviewed and/or for Purge</button>

There is also a matching handle_selected method in my Ruby Controller. Every time I try to use the AJAX method I get the following error:

POST http://localhost:3000/itemresults/handle_selected 404 (Not Found) jquery.js?body=1:9667
  jQuery.ajaxTransport.send jquery.js?body=1:9667
  jQuery.extend.ajax jquery.js?body=1:9212
  jQuery.each.jQuery.(anonymous function) jquery.js?body=1:9358
  callHandleSelected processed_item.js?body=1:37
  onclick

In case you need it, here is the controller method:

def handle_selected
  review_list = params[:review]
  purge_list = params[:purge]
  review_list.each do |item|
    item.split("_")
    proc_item = ProcessedItem.find(item[1])
    proc_item.reviewed = true;
    proc_item.save!
  end
  purge_list.each do |item|
    item.split("_")
    proc_item = ProcessedItem.find(item[1])
    proc_item.purge = true;
    proc_item.save!
  end
  redirect_to processed_items_path()
  #add alert
end

Upvotes: 0

Views: 1273

Answers (2)

user3254621
user3254621

Reputation: 29

A combination of the comments on my initial post answered the question. I took out the redirect_to line and replaced it with this:

respond_to do |format|
  format.js {render inline: "location.reload();" }
end

I was getting the 404 error because I was trying to load objects incorrectly as Baloo pointed out. The new (relevant) code looks like this:

review_list.each do |item|
  id = item.split("_")[1]
  proc_item = ProcessedItem.find(id)

Thanks all!

Upvotes: 0

Max Williams
Max Williams

Reputation: 32933

I think the problem is just that you need a leading slash on your request url:

$.post('itemresults/handle_selected' ...

should be

$.post('/itemresults/handle_selected'

Without the leading slash, it will add the url onto the end of the current page url.

EDIT: you should put a leading slash on the path in your routes.rb file as well. I think that rails "forgives" you for not doing this but i'm not sure: either way you should do it properly, ie with the leading slash.

Upvotes: 1

Related Questions