# Call a controller method from Javascript

**URL:** <https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798>\
**Category:** rubyonrails-talk\
**Created:** [November 26, 2012, 9:27am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798 "2012-11-26T09:27:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 9:27am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/1 "2012-11-26T09:27:53Z")

</div>

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', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'post', &nbsp;&nbsp;&nbsp;&nbsp;parameters: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu\_id: val, &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); }

And in my sessioncontroller.rb

def test\_call\_cont &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [November 26, 2012, 9:39am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/2 "2012-11-26T09:39:27Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 9:48am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/3 "2012-11-26T09:48:25Z")

</div>

Hi Colin, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [November 26, 2012, 10:00am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/4 "2012-11-26T10:00:53Z")

</div>

> Hi Colin, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 10:16am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/5 "2012-11-26T10:16:21Z")

</div>

Colin, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 10:27am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/6 "2012-11-26T10:27:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [November 26, 2012, 12:00pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/7 "2012-11-26T12:00:36Z")

</div>

> Colin, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 12:09pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/8 "2012-11-26T12:09:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Norbert\_Melzer](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@Norbert\_Melzer](https://discuss.rubyonrails.org/u/Norbert_Melzer)\
**Post date:** [November 26, 2012, 12:14pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/9 "2012-11-26T12:14:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 26, 2012, 12:19pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/10 "2012-11-26T12:19:41Z")

</div>

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', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'post', &nbsp;&nbsp;&nbsp;&nbsp;parameters: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu\_id: val &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); }

\</script\>

---

<div class="post-metadata">

**Author:** ![Jim\_Ruther\_Nill](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jim\_Ruther\_Nill](https://discuss.rubyonrails.org/u/Jim_Ruther_Nill)\
**Post date:** [November 26, 2012, 12:50pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/11 "2012-11-26T12:50:08Z")

</div>

> 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', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'post', &nbsp;&nbsp;&nbsp;&nbsp;parameters: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu\_id: val &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); }
> 
> \</script\>

Are you sure that you're using prototype?

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [November 26, 2012, 1:31pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/12 "2012-11-26T13:31:37Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [November 26, 2012, 2:48pm UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/13 "2012-11-26T14:48:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 4:32am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/14 "2012-11-27T04:32:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 4:34am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/15 "2012-11-27T04:34:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jim\_Ruther\_Nill](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jim\_Ruther\_Nill](https://discuss.rubyonrails.org/u/Jim_Ruther_Nill)\
**Post date:** [November 27, 2012, 4:39am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/16 "2012-11-27T04:39:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 4:44am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/17 "2012-11-27T04:44:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 4:46am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/18 "2012-11-27T04:46:21Z")

</div>

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

Nikhil

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 4:51am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/19 "2012-11-27T04:51:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [November 27, 2012, 5:01am UTC](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798/20 "2012-11-27T05:01:55Z")

</div>

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();

});

&nbsp;&nbsp;&nbsp;&nbsp;$.contextMenu({

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

function test\_call(val) { alert(val); new Ajax.Request('/sessions/test\_call\_cont', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'post', &nbsp;&nbsp;&nbsp;&nbsp;parameters: { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu\_id: val &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); }

And my controller code is

def test\_call\_cont &nbsp;&nbsp;&nbsp;puts "Printing from Controller" end

Nikhil

[Next page](https://discuss.rubyonrails.org/t/call-a-controller-method-from-javascript/65798.md?page=2)
