Javascript call in Controller

1,472 views
Skip to first unread message

Fernando Leandro

unread,
Jan 16, 2011, 5:14:57 PM1/16/11
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Jan 16, 2011, 5:27:35 PM1/16/11
to rubyonra...@googlegroups.com
On 16 January 2011 22:14, Fernando Leandro

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

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

> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>

Fernando Leandro

unread,
Jan 16, 2011, 5:38:23 PM1/16/11
to rubyonra...@googlegroups.com
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 <cla...@googlemail.com>

Colin Law

unread,
Jan 17, 2011, 3:06:16 AM1/17/11
to rubyonra...@googlegroups.com
On 16 January 2011 22:38, Fernando Leandro
<fernandol...@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

Bob O

unread,
Jan 17, 2011, 10:58:33 AM1/17/11
to Ruby on Rails: Talk
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:38 pm, Fernando Leandro <fernandoleandro1...@gmail.com>
wrote:
> 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 <clan...@googlemail.com>
>
> > On 16 January 2011 22:14, Fernando Leandro
> > <fernandoleandro1...@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
>
> > > 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
>
> > > --
> > > 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 rubyonra...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > rubyonrails-ta...@googlegroups.com<rubyonrails-talk%2Bunsu...@googlegroups.com>
> > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > --
> > 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 rubyonra...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-ta...@googlegroups.com<rubyonrails-talk%2Bunsu...@googlegroups.com>
> > .

Fernando Leandro

unread,
Jan 17, 2011, 12:17:11 PM1/17/11
to rubyonra...@googlegroups.com

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 <hcin...@gmail.com>
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.

Robert Walker

unread,
Jan 17, 2011, 12:34:27 PM1/17/11
to rubyonra...@googlegroups.com
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]).

--
Posted via http://www.ruby-forum.com/.

Fernando Leandro

unread,
Jan 17, 2011, 12:46:59 PM1/17/11
to rubyonra...@googlegroups.com
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 <li...@ruby-forum.com>

--

Walter Lee Davis

unread,
Jan 17, 2011, 2:20:10 PM1/17/11
to rubyonra...@googlegroups.com

On Jan 17, 2011, at 12:46 PM, Fernando Leandro wrote:

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

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

>
> Thanks,
>
> Fernando
>
> 2011/1/17 Robert Walker <li...@ruby-forum.com>
> 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]).
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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-
> ta...@googlegroups.com.


> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>
>
>
> --
> 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-
> ta...@googlegroups.com.

David Kahn

unread,
Jan 17, 2011, 2:28:36 PM1/17/11
to rubyonra...@googlegroups.com
On Sun, Jan 16, 2011 at 4:14 PM, Fernando Leandro <fernandol...@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.

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


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.
 
There is a way to do something like this?

Thanks

Fernando

--
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 rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages