Hello guys,
I would like to know if i can call a javascript function directly from
my
controller and get the return of the function.
for example:
And than i would like in my controller to do something like this:
#controller
render :text => call “test();”
There is a way to do something like this?
Thanks
Fernando
On 16 January 2011 22:14, Fernando L.
[email protected] wrote:
Hello guys,
I would like to know if i can call a javascript function directly from my
controller and get the return of the function.
No, the controller is written in Ruby and runs on the server.
Javascript runs in the browser. Why do you want to write the function
in javascript, why not just use Ruby?
Colin
On 16 January 2011 22:38, Fernando L.
[email protected] wrote:
Hello,
Please don’t top post, it makes it difficult to follow the thread.
Insert your reply at appropriate points in previous post. Thanks.
Because i need to get the content of a div in the page.
Think about when the controller and view code is run. The
controller/view code is completely run before it is sent to the
browser. Therefore you cannot access anything in a div in the
controller, because the div does not exist until it is sent to the
browser. If you know what is going to be in the div then you can
use that in the controller. Once the view has been built it is sent
to the browser and then you can only run javascript.
Colin
To clarify, Are you trying to get these values before THIS page is
set? and if so what is creating these values? Would you not already
have the data?
If you are trying to get values via javascript for the next request
could you not use javascript on the page to set params on what sets
the search?
im a little confused on your flow. please clarify a bit more and I may
be able to assist.
On Jan 16, 3:38pm, Fernando L. [email protected]
Hello,
Because i need to get the content of a div in the page.
I need to do this
var currentEvents = jQuery(“.wc-cal-event”);
currentEvents.each(function(index, element) {
var calEvent = jQuery(this).data(“calEvent”);
alert(calEvent.start);
alert(calEvent.end);
});
So i need to call a function to return to me this calEvent values, to
use in
a search.
There is a way to do this with ruby instead of javascript?
Thanks
Fernando
2011/1/16 Colin L. [email protected]
Robert,
Can you give me an example of how to do this
“The same way you always send params to your controllers. Either put
them
in the query string (for GET requests) or put them in the request body
as form data (for POST or PUT requests).”
I dont know how to do this manually, usually i just always send with a
rails
helper.
Thanks,
Fernando
2011/1/17 Robert W. [email protected]
Fernando L. wrote in post #975487:
How can i do this? How can i send params for my controller with
javascript?
The same way you always send params to your controllers. Either put them
in the query string (for GET requests) or put them in the request body
as form data (for POST or PUT requests).
Then access them from the params hash in the controller
(params[:some_value]).
I want to get this value AFTER the page is set. The user creates some
divs
using ajax ( its a calendar ),
and than when he click in the search button, i want to get the values
that
the user choiced in this div`s and use then in my controller to make the
search.
But as you said:
“If you are trying to get values via javascript for the next request
could you not use javascript on the page to set params on what sets
the search?”
How can i do this? How can i send params for my controller with
javascript?
Thanks
2011/1/17 Bob O [email protected]
On Sun, Jan 16, 2011 at 4:14 PM, Fernando L. <
[email protected]> wrote:
}
And than i would like in my controller to do something like this:
#controller
render :text => call “test();”
What are you trying to achieve? Is it that you have a js library and
don’t
want to port it to ruby? There is no ‘native’ way to do this but may be
possible, just as certain test libraries can call js functions, but
first to
clear is whether you really need to do this.
On Jan 17, 2011, at 12:46 PM, Fernando L. wrote:
a rails helper.
Are you using Prototype or jQuery? In Prototype, you get the content
of form elements using
var data = $F(‘theIdOfTheFormElement’);
If you’re not putting the user’s data in a form field (even a hidden
one) then you will need to do something hacky like
var data = $(‘theIdOfTheElement’).innerHTML.stripTags();
Then to send it to your controller, you do something like this:
new Ajax.Request(’/url/of/endpoint’,{parameters:{your_rails_parameter:
data}});
To do the same, but update a portion of the page with whatever HTML
your Rails request returns, you substitute Updater for Request, and
add the ID of the element you need to update:
new Ajax.Updater(‘theIdOfTheElementYouWantToUpdate’, ‘/url/of/
endpoint’, {parameters:{your_rails_parameter: data}});
Note that Updater does not replace the outer box, only the contents of
the box you indicate with your ID. Also, Updater does not update form
fields (you can replace them, but the correct thing to do is to use
Ajax.Request and a callback function onSuccess to update anything
besides a DIV or another container element.
Walter