开发者

apply link_to_remote from a jQuery handler in Rails

开发者 https://www.devze.com 2023-01-31 04:49 出处:网络
I have a game with 5 boxes and when a box is clicked I want to send an ajax request to a controller that in effect is exactly like link_to_remote. I need to send a hash of informatoin to a definition

I have a game with 5 boxes and when a box is clicked I want to send an ajax request to a controller that in effect is exactly like link_to_remote. I need to send a hash of informatoin to a definition in a controller but don't need anything to get sent back or even updated.

What the following jQuery/js mess does is render a function which either correct or incorrect. From those functions I want to be able to send an ajax request to some controller with a hash of information.

$('#tones a').click(function(e) {
        if (playGame) { 
        id = this.id.replace('t', '')
            if (correctCharacter[1] == id) {
                correct();
            } else {
                incorrect();
            }
        addCharacters();
        }
    });
})

I know link_to_remote has the following syntax and this link is not really related at all:

# Generates: <a href="#" onclick="new Ajax.Updater('posts', '/blog/destroy/3', {asynchronous:true, evalScripts:true});
link_to_remote "Delete this post", :update => "posts", :url => { :action => "destroy", :id => post.id }

If you could show me how to make this link work in the latter function. I just need one ajax link or request whatever is right that goes to a controller and sends a hash of information.

I want to put it in the following block. If it is correct then it will trigger and if it is not correct then it will trigger.

if (correctCharacter[1] == id) {
                correct();
            } else {
                incorrect();
            }

Here is js source from a link_to_remote I put on a page just to see the security stuff. It's not really related to where I want to post information but it is related to my spe开发者_运维知识库cific application.

<a href="#" onclick="new Ajax.Updater('posts', '/groups/new', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('jQx+jlICHcZ3X6SPOcNUNBPJF8qLGDarO/nasHfWkvQ=')}); return false;">Test</a>


This is what I came up with. It works except it isn't posting to the correct url.

$.ajax({
   type: "POST",
   url: "analytics/test",
   data: 'correct=' + correct_or_incorrect + '&id=' + correctCharacter[3] + "'"
  });
0

精彩评论

暂无评论...
验证码 换一张
取 消