Topic: AJAX Routing Problem?

I will admit that Rails routing has me completely, um, stymied? flummoxed?

I have a transactions controller, with an action I've just called trans_item for the time being. 

In the routes.rb file, I put this as the top line, before the resource definitions:

match ':controller/:action'

Then, in a web page, I have a drop down that calls the following bit of code onChange:

function myAjaxTest() {
    
    
    var xhr = new XMLHttpRequest();
    xhr.open("get", "transactions/trans_item", false);
    xhr.send(null);
    
    alert(xhr.responseText);

}

In the response text I get (besides all the basic html stuff):

<h1>Routing Error</h1>
<p><pre>No route matches &quot;/transactions/transactions/trans_item&quot;</pre></p>

So I'm fundamentally confused as to why the trans_item code doesn't appear to be found.  I'm also confused as to why the "/transactions/transactions" bit since I'm never constructing that URL that I can see.

Any help would be greatly appreciated.

Re: AJAX Routing Problem?

I could be wrong, but from this

/transactions/transactions/trans_item

it looks like you called transactions twice.

Maybe try

xhr.open("get", "trans_item", false);

just something that I know I would try if I saw that error

- Ben

Re: AJAX Routing Problem?

Definitely a good thought.  I had tried that, but the response is the same, except the "/transactions/transactions/" part of the response is missing - which is really weird.

Re: AJAX Routing Problem?

But then you got me thinking about the whole thing some more.  Since I'm at localhost:3000/transactions when I call the javascript, should I change the url to ../transactions/trans_item, and it works (of course its mad at me now because there is no template for it to render, but I'm good with that smile ).

Re: AJAX Routing Problem?

also, try

xhr.open("get", "/transactions/trans_item", false);

Begin with the slash

- Ben

Re: AJAX Routing Problem?

Yeah, that works, too, although I had tried it earlier and failed.  I think I had two problems.  One was the URL I was using, and the other is where in the routes file I placed the match command.  I had it after the resources call and so Rails was, I think, hitting the default route for verb Get and path /transactions/:id, thus looking at trans_item as an id.