0

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
5
  • What is in handle_selected in your controller? Commented Jul 2, 2014 at 13:54
  • Edited my post to include that method. Commented Jul 2, 2014 at 13:56
  • Your controller action doesn't respond to a javascript request, it should render something in context of json too. Also, make sure that this is the correct path for POST request. You specified that in config/routes.rb right? Commented Jul 2, 2014 at 14:02
  • 1
    Yep, ajax call can't do a redirect_to Commented Jul 2, 2014 at 14:03
  • ProcessedItem.find(item[1]) will throw a 404 if it doesn't find a ProcessedItem Commented Jul 2, 2014 at 14:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

1 Comment

Does anyone know how to keep that respond_to block from popping up a dialog box? It opens up a box that says, "location.reload();" and you have to click OK before the page actually refreshes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.