Call a controller method from Javascript

Jim ruther Nill wrote in post #1086597:

> ok. Are you using the asset pipeline? Also, what browser are you > using? > I suggest you use chrome because it has a default developer tool. Look > at the js errors when you load the page. If there is nothing on the > log file and there's no response on the page, it most likely is an error > with javascript. >

Jim, I am using firefox 11. I am afraid I cant change my browser. And sorry, how do I look for the js errors? I got nothing in the log file.

install firebug. after installing firebug, open the firebug console and reload the page. you should see the js errors on firebug after that.

Hey Guys, let me explain you all my scenario where I am stuck.

I got a table filled with cells. When I right click on any cell, I got to display a context menu and then call a controller method on left clicking the context menu. I got to send in a parameter to my controller code and return what controller sends back. I am using jquery right

you're using both prototype and jquery? why do you need both of them?

Jim,

If I put in prototype, my jquery doesnt work. So can i remove prototype? And yes Ill install FIREBUG.

Nikhil

Jim,

If I put in prototype, my jquery doesnt work. So can i remove prototype? And yes Ill install FIREBUG.

yes. remove prototype. they have conflict since they both use $. there's a way around this but if you don't need both of them, it'd make your life easier.

Look at jQuery.ajax() | jQuery API Documentation for implementing ajax on jquery

Jim,     I have removed the prototype.js file from app/assets/javascripts folder. I will look into the link which you have posted.

Thanks,

Nikhil

Colin,      I have installed firebug for my Browser and after I left click my right click menu option, I get the alert in the firebug console. No errors there too. But how do I call the controller method?

Nikhil

Colin,       I have not put any javascript in application.js. If I have to put any, what should that script be trying acheive?

Nikhil

Jim,    I have removed the prototype.js file from app/assets/javascripts folder. I will look into the link which you have posted.

Thanks,

Nikhil

The script that you posted originally was written using Prototype conventions, not jQuery. If you substitute jQuery for Prototype, you also need to alter your code to be jQuery-compliant.

Now if you have more Prototype than jQuery in your hand-written code, you can make Rails use Prototype in place of jQuery for all of its core unobtrusive functions (:remote => true and so forth). Just make sure you have the proper (prototype) version of rails.js in your project, and only include prototype.js (not both prototype.js and jquery.js) in your application.html.erb template. Make a scratch Rails project with:

  rails new foo -j=prototype

Have a look in that project for the relevant files.

Walter

Walter,       I will change the prototype to jquery. Thanks.

Nikhil

Finally got it working. I thank you all for your support, time and patience.

$(document).ready(function() { var currentCellText;/*this variable will have the clicked cell value*/ $("tbody td").mousedown(function(e) { currentCellText = $(this).text();

});

    $.contextMenu({

        selector: '.context-menu-one',         callback: function(key, options) {             var m = "clicked: " + currentCellText;            window.console && console.log(m)|| test_call(currentCellText);         },         items: {             "Menu 1": {name: "Menu 1"},             "Menu 2": {name: "Menu 2"},        }     });     });

function test_call(val) { $.ajax({   type: 'GET',   url:'/leave',   data:{ foo: val },   success:function(data){ alert(data); } }); }

Nikhil