Forum: Ruby on Rails Javascript call in Controller

9a4a83102ca0547963b5b393e839ecad?d=identicon&s=25 Fernando Leandro (Guest)
on 2011-01-16 23:16
(Received via mailing list)
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:

<script>
function test() {
    return 'Hello World';
}
</script>

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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2011-01-16 23:29
(Received via mailing list)
On 16 January 2011 22:14, Fernando Leandro
<fernandoleandro1991@gmail.com> 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
9a4a83102ca0547963b5b393e839ecad?d=identicon&s=25 Fernando Leandro (Guest)
on 2011-01-16 23:39
(Received via mailing list)
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 Law <clanlaw@googlemail.com>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2011-01-17 09:08
(Received via mailing list)
On 16 January 2011 22:38, Fernando Leandro
<fernandoleandro1991@gmail.com> 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
Dd582d81c5b7346a594e76d0163fa593?d=identicon&s=25 Bob O (Guest)
on 2011-01-17 16:59
(Received via mailing list)
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 Leandro <fernandoleandro1...@gmail.com>
9a4a83102ca0547963b5b393e839ecad?d=identicon&s=25 Fernando Leandro (Guest)
on 2011-01-17 18:18
(Received via mailing list)
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 <hcinsight@gmail.com>
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2011-01-17 18:34
Fernando Leandro 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]).
9a4a83102ca0547963b5b393e839ecad?d=identicon&s=25 Fernando Leandro (Guest)
on 2011-01-17 18:48
(Received via mailing list)
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 Walker <lists@ruby-forum.com>
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2011-01-17 20:21
(Received via mailing list)
On Jan 17, 2011, at 12:46 PM, Fernando Leandro 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
6799ecb7745167d2590d85c4ed25d985?d=identicon&s=25 David Kahn (dkahn)
on 2011-01-17 20:29
(Received via mailing list)
On Sun, Jan 16, 2011 at 4:14 PM, Fernando Leandro <
fernandoleandro1991@gmail.com> wrote:

> }
> </script>
>
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.