How to call a java function in rubyonrails method

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.

It’s very simple, just install a JVM and invoke your class from ruby
like
this:

java -cp #{YOUR_JARPATH} #{YOUR_JAVACLASS} #{YOUR_ARGUMENTS}

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:

Call Me!.

*m

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?

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:

Call Me!.

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

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


<%end%>
<% if @mobile%>
It’s already stored.
<%else%>
Congratulation. Now you’re online.
<%end%>

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

Thank you so much!
Now the situation is quite clear.
Thanks,
M#

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.