Call a controller method from Javascript

Hey guys, I am trying to build an web app. I am running Rails 3. I just want to make a call to a controller method from my javascript(view part). Most of the links says AJAX. But I dont know whether its my search part on the internet causing me problems, but I just could not find a simple way of calling a Controller Method from Javascript.

My javascript function: home.html.erb

function test_call(val) { alert(val); new Ajax.Request('/SessionsController/test_call_cont', {     method: 'post',     parameters: {       menu_id: val,     }   }); }

And in my sessioncontroller.rb

def test_call_cont                 puts "Printing from Controller" end

Where am I going wrong, should I have to include any js files, or is my syntax wrong? Totally confused.

Please do guide me.

Regards, Nikhil

Hey guys, I am trying to build an web app. I am running Rails 3. I just want to make a call to a controller method from my javascript(view part). Most of the links says AJAX. But I dont know whether its my search part on the internet causing me problems, but I just could not find a simple way of calling a Controller Method from Javascript.

My javascript function: home.html.erb

function test_call(val) { alert(val); new Ajax.Request('/SessionsController/test_call_cont', {

Ajax.Request takes a url. Is '/SessionsController/test_call_cont' a valid url? Perhaps '/sessions/test_call_cont' is what you want. Not thought that you are not 'calling a controller method' but are initiating a request on that url.

Colin

Hi Colin,         Im sorry but in the url, I am supposed to put up the path of my controller method right?

Yes, my controller method is under main-application/app/views/sessions/.

So should I put it up like sessions/test_call_cont in the request? I will also have to pass few parameters later on to that controller method and render new page based on some operations in that controller method(test_call_cont).

Regards, Nikhil

Hi Colin,         Im sorry but in the url, I am supposed to put up the path of my controller method right?

Yes, my controller method is under main-application/app/views/sessions/.

You said earlier that the code is in sessions_controller.rb (actually you said sessioncontroller.rb, but I assumed you meant sessions_controller.rb) which should be in app/controllers, not app/views.

So should I put it up like sessions/test_call_cont in the request?

Yes, you will also have to set up the route in routes.rb of course.

I will also have to pass few parameters later on to that controller method and render new page based on some operations in that controller method(test_call_cont).

That is ok that is what controllers are for, but you should think of it as an action, not a method (though it is a method of the controller of course).

Colin

Colin,       Sorry again. It was a typing mistake I believe. My controller is rightly placed in app/controllers. Though I have given the url as /sessions/test_call_cont, Nothing seems to be happening. I am not getting any error but neither I am getting the required output. Where can I have gone wrong?

Nikhil

And I have also routed the method in the routes.rb. I have invoke that url in routes.rb. But how can I achieve that from a javascript function is my concern. Awaiting your thoughts on this.

Nikhil

Colin,       Sorry again. It was a typing mistake I believe. My controller is rightly placed in app/controllers. Though I have given the url as /sessions/test_call_cont, Nothing seems to be happening. I am not getting any error but neither I am getting the required output. Where can I have gone wrong?

Do you see anything in log/development.log when you make the call?

Colin

No Colin. I kept track of line numbers in development.log . After I make that click, there seems to be no action happening.

Just a quickshot, but is your javascript function really triggered when clicking the link? Perhaps you could show that part of the view?

If I put in an alert before the ajax call, the alert gets displayed. This is what you meant you by triggering the javascript right?

<script type="text/javascript">

function test_call(val) { alert(val); new Ajax.Request('/sessions/test_call_cont', {     method: 'post',     parameters: {       menu_id: val     }   }); }

</script>

If I put in an alert before the ajax call, the alert gets displayed. This is what you meant you by triggering the javascript right?

<script type="text/javascript">

function test_call(val) { alert(val); new Ajax.Request('/sessions/test_call_cont', {     method: 'post',     parameters: {       menu_id: val     }   }); }

</script>

Are you sure that you're using prototype?

And I have also routed the method in the routes.rb. I have invoke that url in routes.rb. But how can I achieve that from a javascript function is my concern. Awaiting your thoughts on this.

Nikhil

What happens when you type that URL into a browser and try to visit it? You should see something, unless your controller does not include any handlers for a regular http request.

Walter

Do you see an error if you run with a js debugger such as firebug in firefox? That is assuming you have checked that you are including prototype.js as suggested by Jim.

Colin

Jim ruther Nill wrote in post #1086470:

Are you sure that you're using prototype?

Jim,

Yes I have put the latest prototype.js file in the app/assets/javascripts folder.

Nikhil

Walter Davis wrote in post #1086483:

What happens when you type that URL into a browser and try to visit it? You should see something, unless your controller does not include any handlers for a regular http request.

Walter

Walter,

When I hit the url, I found out I get this error:

Missing template sessions/test_call_cont, application/test_call_cont with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}

Have I missed any template?

Nikhil

Jim ruther Nill wrote in post #1086470:

> Are you sure that you're using prototype?

Jim,

Yes I have put the latest prototype.js file in the app/assets/javascripts folder.

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 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.

Nikhil

Jim, Forgot to mention on asset pipeline. Sorry again, I did not get by what you meant there.

Nikhil

Walter,

The complete error message in my browser when the hit the URL is

Template is missing

Missing template sessions/test_call_cont, application/test_call_cont with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

Searched in: * "/home/nikhil/nikhil_ror/main_application/app/views"

Have I given any wrong path?

Nikhil

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 click for my context menu. I have the clicked cell value. I just need to call a ruby controller method.

This is my home.html.erb code:

$(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) { alert(val); new Ajax.Request('/sessions/test_call_cont', {     method: 'post',     parameters: {       menu_id: val     }   }); }

And my controller code is

def test_call_cont    puts "Printing from Controller" end

Nikhil