Forum: Ruby on Rails How to call a java function in rubyonrails method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matteo (Guest)
on 2006-04-14 17:55
Hi everyone,
I'm Italian and sorry for my english mistakes...

I'm developing a web-site using rubyonrails.
It's REALLY COOL!
But I'm obliged to re-use some javascript functions.
Do you know where I can get the right documentation (and examples) in
order to integrate these two different environments?

Thanks,
Matteo.
Roberto S. (Guest)
on 2006-04-14 19:05
(Received via mailing list)
It's very simple, just install a JVM  and invoke your class from ruby
like
this:

`java -cp #{YOUR_JARPATH} #{YOUR_JAVACLASS}  #{YOUR_ARGUMENTS}`
Manuel H. (Guest)
on 2006-04-14 19:24
(Received via mailing list)
Am 14.04.2006 um 15:55 schrieb Matteo:

> Hi everyone,
> I'm Italian and sorry for my english mistakes...
>
> I'm developing a web-site using rubyonrails.
> It's REALLY COOL!
> But I'm obliged to re-use some javascript functions.
> Do you know where I can get the right documentation (and examples) in
> order to integrate these two different environments?

Do you mean JavaScript (aka ECMAScript, JScript) or Java (as in Sun
Java, J2EE and Applets).

The latter case has already been answered and the earlier case -
well. What about this in your templates:

<a href="#" onclick="javascript:theFunctionCall();">Call Me!</a>.

*m
Matteo D. (Guest)
on 2006-04-14 19:57
> Do you mean JavaScript (aka ECMAScript, JScript) or Java (as in Sun
> Java, J2EE and Applets).
>
> The latter case has already been answered and the earlier case -
> well. What about this in your templates:
>
> <a href="#" onclick="javascript:theFunctionCall();">Call Me!</a>.
>
> *m

Yes, it's the last case!
In particolar I have to call this function from the controller, not from
the view. Is it possible? And what about the arguments? And what should
I do to handle the return result?

Thanks you a lot!
M#
unknown (Guest)
on 2006-04-15 04:21
(Received via mailing list)
>> But I'm obliged to re-use some javascript functions.
>> Do you know where I can get the right documentation (and examples) in
>> order to integrate these two different environments?
> In particolar I have to call this function from the controller, not from
> the view. Is it possible? And what about the arguments? And what should
> I do to handle the return result?

Can you explain a little more about what these existing functions are,
how
they are supposed to work, how you invoked them in the past, etc?
Matteo D. (Guest)
on 2006-04-15 18:48
> Can you explain a little more about what these existing functions are,
> how
> they are supposed to work, how you invoked them in the past, etc?

It's a little bit difficult to explain, but I'll try with an example.
Now my view is just made with a simple input box where you can put your
address. This input box action calls the javafunction.
My javascript function has 3 different parts for 3 different steps.
1) The first one has to open an xml file, where all the information
about the people are stored.
2) The second one has to fill some variables from this file, for example
the phone number of the user who is login.
3) And the last one has to use these variables in order to create a
'result_div'.

Now I have to reach some other goals. In particolar the final goal is to
have a query in my database in order to understand if the phone number
is already stored. According to the result I have to show a different
view. Then I have to invoke the second javascript function in order to
complete the 'result_div'.
So I thought to split the original java function in 2 parts and to make
a wrapper of the original java function like this.

---
(controller)
def wrapper(address)
    phone_number=javascript:YourNumber(filename,address);
    @mobile.find(:first, conditions=>["phone_number=?)",phone_number])
    javascript:FunctionCall();
end
---
(view)
<%start_form_tag :action => 'wrapper'%>
<%= text_field_tag 'address'%></p>
<%end%>
<% if @mobile%>
It's already stored.
<%else%>
Congratulation. Now you're online.
<%end%>
<div id="result_div"> </div>

So I hope you understood. I did my best... As you understood my case is
more complicated and I REALLY DIDN'T want to bore you! But if you read
everything THANK YOU SO MUCH!!!
So I have the follow questions: in order to make a wrapper I need to
know
-how may I pass the argument ADDRESS to the method wrapper.
-how may I call the javafunction from the method.

THANK YOU SO MUCH, and when the site will be online you will be the
first to know!
FEEL FREE TO TELL ME YOUR OPINION!
Matteo
unknown (Guest)
on 2006-04-15 22:15
(Received via mailing list)
> Now I have to reach some other goals. In particolar the final goal is to
> have a query in my database in order to understand if the phone number
> is already stored. According to the result I have to show a different
> view. Then I have to invoke the second javascript function in order to
> complete the 'result_div'.

OK, I think I see basically what you're trying to do.  You may be able
to use
something like ruby-js
    http://raa.ruby-lang.org/project/ruby-js/
    http://www.rubyist.net/~tamura/ruby/ruby-js/
to run your Javascript function server-side.

My first stab at a problem like this would probably punt on reuse and
port;
looking something like this perhaps:

View
    <% form_remote_tag :url => {:action => 'wrapper'},
                                    :update => mobile_div %>
      <%= text_field_tag 'address'%>
      <%= submit_tag %>
    <% end_form_tag %>

    <div id="mobile_div"> </div>

    <div id="result_div"> </div>

Controller
    def wrapper(address)
       phone_number = new_ruby_xml_parseing_function(address);
       if @mobile.find(:first,
conditions=>["phone_number=?)",phone_number])
         render_text "It's already stored."
       else
         render_text "Congratulation. Now you're online."
       end
    end

For the result_div, depending on what it needs to do my first thoughts
would be to either use the forms :complete callback to run the existing
javascript function or build the result_div server side in ruby and use
an
rjs template to fill it in.
Matteo D. (Guest)
on 2006-04-15 23:41
Thank you so much!
Now the situation is quite clear.
Thanks,
M#
This topic is locked and can not be replied to.