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
on 2012-11-26 10:27
on 2012-11-26 10:41
On 26 November 2012 09:27, nikhil rn <lists@ruby-forum.com> wrote: > 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
on 2012-11-26 10:48
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
on 2012-11-26 11:02
On 26 November 2012 09:48, nikhil rn <lists@ruby-forum.com> wrote: > 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
on 2012-11-26 11:16
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
on 2012-11-26 11:27
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
on 2012-11-26 13:02
On 26 November 2012 10:16, nikhil rn <lists@ruby-forum.com> wrote: > 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
on 2012-11-26 13:09
No Colin. I kept track of line numbers in development.log . After I make that click, there seems to be no action happening.
on 2012-11-26 13:15
Just a quickshot, but is your javascript function really triggered when clicking the link? Perhaps you could show that part of the view? Am 26.11.2012 13:09 schrieb "nikhil rn" <lists@ruby-forum.com>:
on 2012-11-26 13:19
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>
on 2012-11-26 13:51
On Mon, Nov 26, 2012 at 8:19 PM, nikhil rn <lists@ruby-forum.com> wrote: > parameters: { > menu_id: val > } > }); > } > > </script> > Are you sure that you're using prototype? > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-26 14:34
On Nov 26, 2012, at 5:27 AM, nikhil rn wrote: > 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
on 2012-11-26 15:49
On 26 November 2012 12:19, nikhil rn <lists@ruby-forum.com> wrote: > parameters: { > menu_id: val > } > }); > } > > </script> 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
on 2012-11-27 05:32
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
on 2012-11-27 05:34
Walter Davis wrote in post #1086483: > On Nov 26, 2012, at 5:27 AM, nikhil rn wrote: > > 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
on 2012-11-27 05:40
On Tue, Nov 27, 2012 at 12:32 PM, nikhil rn <lists@ruby-forum.com> wrote: > 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. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-27 05:44
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
on 2012-11-27 05:46
Jim, Forgot to mention on asset pipeline. Sorry again, I did not get by what you meant there. Nikhil
on 2012-11-27 05:51
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
on 2012-11-27 06:01
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
on 2012-11-27 06:41
On Tue, Nov 27, 2012 at 12:44 PM, nikhil rn <lists@ruby-forum.com> wrote: > 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. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-27 06:42
On Tue, Nov 27, 2012 at 1:01 PM, nikhil rn <lists@ruby-forum.com> wrote: > 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? > currentCellText = $(this).text(); > }, > alert(val); > And my controller code is > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-27 06:43
Jim, If I put in prototype, my jquery doesnt work. So can i remove prototype? And yes Ill install FIREBUG. Nikhil
on 2012-11-27 06:47
On Tue, Nov 27, 2012 at 1:43 PM, nikhil rn <lists@ruby-forum.com> wrote: > 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 http://api.jquery.com/jQuery.ajax/ for implementing ajax on jquery > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-27 06:54
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
on 2012-11-27 09:50
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
on 2012-11-27 10:28
Colin,
I have not put any javascript in application.js. If I have to put
any, what should that script be trying acheive?
Nikhil
on 2012-11-27 15:24
On Nov 27, 2012, at 12:54 AM, nikhil rn wrote: > 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
on 2012-11-28 10:38
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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.