Re: Where to define function in rhomobile..?

421 views
Skip to first unread message

Rob

unread,
Aug 22, 2012, 6:39:01 PM8/22/12
to rhom...@googlegroups.com
Yes, you can call a controller method from javascript using asynchttp.  I do it all through my app...




On Wednesday, August 22, 2012 2:28:48 PM UTC-4, abhay wrote:
Hi,
 
I am trying to create Login Screen, which contains two edit text fields(Username and Password) and two buttons(Login and Register). When the user clicks on Login Button, I am trying to call login.erb and when the user tries to click on the button Register i tried to call register.erb file.
 
For that i tried something  like this when the user clicks on button:
 

      <a  href="<%= url_for :controller => :Settings, :action => :login%>"><button value ="Login"/></a>

      <a href="<%= url_for :controller => :Settings, :action => :register%>"><button value ="Register"/></a>

 

It's working fine but, before calling login.erb file where do I have to do validations for username and password..? . Actually I had tried validations in javascript it's working fine but from javascript we cannot call ruby syntax(the above shown two statements)....?

 

Do I need to do valdations using ruby or what..?but I am very new to rho mobile, I am not aware of ruby..

Can we call ruby fron java script..? Help me out,please....

 

 

vnlakshmi 39

unread,
Aug 23, 2012, 5:49:13 AM8/23/12
to rhom...@googlegroups.com
Hi abhay

try like this
def validation
if @params["celnumber"]== '' && @params["password"]== ''
      Alert.show_popup 'Cellphone number or PIN incorrect.'
      redirect :action => :login
    elsif @valueCellN == false
      Alert.show_popup 'Cellphone number or PIN incorrect.'
      redirect :action => :login
else
redirect :action => :home
end

On Thu, Aug 23, 2012 at 12:05 PM, abhay <abhay....@gmail.com> wrote:
Hi ,
 
Thanks for the reply, Could you please send me some code snippet, bcoz I am new rhomobile and started learning ruby now..
 
I tried something like this,
Onclick of Login button I am calling the function validateLogin() which is defined in script,
 
<script type = "text/javascript">
  function validateLogin(){
       if(condition true){
  //calling here to go to welcome.erb,iflogin success .
     <a href="<%= url_for :controller => :Settings, :action => :welcome%>"></a>
 
}
}
</script>
 
which is not working, How can I call welcome.erb, if login is success...?   
--
You received this message because you are subscribed to the Google Groups "rhomobile" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhomobile/-/oitP75VmYScJ.

To post to this group, send email to rhom...@googlegroups.com.
To unsubscribe from this group, send email to rhomobile+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhomobile?hl=en.

Message has been deleted

Jon Tara

unread,
Aug 26, 2012, 11:26:49 AM8/26/12
to rhom...@googlegroups.com
You can't define a Ruby function in Javascript.

We can't show you what you are doing wrong unless you give us an example.

You can do validation in Javascript, in your Controller, or both. Normally, doing validation in Javascript is considered insecure. However, since Javascript in Rhodes is embedded in an app, doing validation in Javascript is sufficient.

If you do validation in your controller, then you would either use some Ajax to let the user know about invalid input, or you would re-render the page with error messages.

abhay

unread,
Aug 27, 2012, 1:15:39 AM8/27/12
to rhom...@googlegroups.com
Hi,
 
Thanks for the reply.
Below I mentioned my index.erb file, what I am trying over there is, I am having Login screen(index.erb) in which if the username and password equals to 10603542
then I want to take the user to welcome.erb file, I am not knowing how to take the user to welcome.erb file from the javascript function check(form). Please help me out.
 
 
<div data-role="page" data-url="<%=Rho::RhoConfig.start_path%>/">
 
  <div data-role="header" data-position="inline">
    <h1>Splice Test</h1>
    <%if SyncEngine::logged_in > 0 %>
    <a href="<%=url_for:controller=>:Settings,:action=>:do_sync%>"class="ui-btn-left" data-icon="refresh">
      Sync
    </a>
    <a href="<%=url_for:controller=>:Settings,:action=>:logout%>"class="ui-btn-right" data-icon="star">
          Logout
        </a>
    <%else %>
        <a href="<%=url_for:controller=>:Settings,:action=>:login%>"class="ui-btn-right" data-icon="star">Login</a>
    <%end%>
 </div>
 
  <div data-role="content">
      <html>
      <head>
      <title>Login page</title></head>
      <body>
      <h1 style="font-family:ComicSansMs;text-align="center";font-size:20pt;color:#00FF00;>SimpleLoginPage</h1>
     <formname="login">
     Username<inputtype="text"name="userid"/>
     Password<inputtype="password"name="pswrd"/>
     <inputtype="button"onclick="check(this.form)" value="Login"/>
      <input type="reset" value="Cancel"/>
      </form>
 
      <script language="javascript">
    
      function check(form)
     /*functiontocheckuserid&password*/
      {
        /*thefollowingcodecheckeswhethertheentereduseridandpasswordarematching*/
        if(form.userid.value == "10603542" && form.pswrd.value =="10603542")
          {
            alert("LoginSuccessfull");
        // From here I want to call welcome.erb file , I tried by using the below statement but it's not working..
            <a  href="<%=url_for:controller=>:Settings,:action=>:welcome%>"></a>
          }else {
            alert("ErrorPasswordorUsername");
          }
        }
        </script>
        </body>
        </html>
  </div>
 
</div>

Jon Tara

unread,
Aug 27, 2012, 1:17:54 PM8/27/12
to rhom...@googlegroups.com
I don't understand why you want to do password authentication in Javascript. Why don't you do it in the controller, where this is normally done?

When I said that it is OK to do validation in Javascript, I mean like checking a field to see if it is a phone number, has invalid characters, etc.

This is just form submission. Surely there are examples in sample app. I'm sure there is a login example there as well.

I think if you are trying to do password authentication in Javascript, you are missing some fundamental concept of Rhodes or MVC in general. It's quite simple, and you are making it complicated.

You send a form to the server. The server does something with the data. The server sends back a page. In the case of Rhodes, the server in in the device.


abhay

unread,
Aug 27, 2012, 2:11:00 PM8/27/12
to rhom...@googlegroups.com
Hi Jon Tara,
 
Thanks for the reply. To do validations in controller we need to know Ruby right but I am not aware of Ruby, I have to learn.
 
Actually, I am not understanding, how to communicate from .erb file to controller.?
 
 For example as you told, I'm having an index.erb file  which contains an edit field  in which user have to enter phone numner, After entering the phone number if the user clicks on submit button then
I want to validate that number, for that  how to pass the Phonenumber to controller for validation. Means if the user enters the proper phone number then I want to take him to success.erb file else fialure.erb file. How can I achieve this. Help me out please.

Jon Tara

unread,
Aug 27, 2012, 2:47:19 PM8/27/12
to rhom...@googlegroups.com
Well, since Rhodes uses Ruby for mostly (with javascript for UI only) I'd suggest you learn Ruby.

You need to learn the MVC pattern, which is used by Rhodes.

When the user hits the submit button on a form, it's just like submitting a form to a website. You need to write a controller method (in Ruby) to handle the form data.

I think you need to begin at the beginning, and read the documentation.

I'd at least read about App Structure:

http://docs.rhomobile.com/rhodes/application

and User Interface:

http://docs.rhomobile.com/rhodes/ui

If you want to do your project in Javascript, you shouldn't be using Rhodes. Use PhoneGap, or something similar.

Egghead

unread,
Aug 27, 2012, 10:03:36 PM8/27/12
to rhom...@googlegroups.com
I think trying to avoid Ruby while using Rhodes would be a bit ambitious. Many of us are new to Ruby, but it's not very hard to learn, in fact, you might like it :)

vnlakshmi 39

unread,
Aug 31, 2012, 5:30:43 AM8/31/12
to rhom...@googlegroups.com
Please define that code controller.rb file

On Tue, Aug 28, 2012 at 7:33 AM, Egghead <mark.no...@gmail.com> wrote:
I think trying to avoid Ruby while using Rhodes would be a bit ambitious. Many of us are new to Ruby, but it's not very hard to learn, in fact, you might like it :)
--
You received this message because you are subscribed to the Google Groups "rhomobile" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhomobile/-/b-D954Rko_wJ.

Prabu Duraiswamy

unread,
Sep 8, 2012, 1:47:28 AM9/8/12
to rhom...@googlegroups.com
Hi Abhy,

Hope this sample help you to get some idea.

In View page.
login.erb
<script src="/public/jquery/jquery.min.js" type="text/javascript"></script>
<form method="POST" action="<%= url_for :action => :create %>" id="login-form">
  <div id="error" style="display:block">
       <%= @params['notice'] %>
  </div>
  <input id="phonenum" name="authentication[phonenumber]" type="text" placeholder="Phonenumber">
  <input id="password" name="authentication[password]" placeholder = "Password" type="password">
  <button type="submit">Sign In</button>
</form>
<script type="text/javascript">
$(document).ready(function() {
    $("#login-form").submit(function(event) {
        phone= $('#phonenum').val();
        pwd = $('#password').val();
        if (phone == '') {
            $("#error").css("display", "block");
            $('#error').html("Please enter your phone number");
            return false;
        } else if (pwd == '') {
            $("#error").css("display", "block");
            $('#error').html("Please enter your password");
            return false;
        }
    });
});
</script>

In Controller
def create
    if @params['authentication']["phonenumber"] == ''10603542"
        notice = " Login successful"
        WebView.navigate(url_for(:action => :welcome, :controller => :Home, :notice=> notice))
    else
        notice = "Invalid access try again."
        redirect :action => :login, :query=>{:notice => notice }
    end
end

--
Regards,
Prabu D


--
You received this message because you are subscribed to the Google Groups "rhomobile" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhomobile/-/5LaFqG6MgxYJ.

Egghead

unread,
Oct 11, 2012, 2:36:59 AM10/11/12
to rhom...@googlegroups.com
Just tested this in Win Mobile. On not entering any Phone Number and submitting the form, I only get the controller (server-side) validation message : "Invalid access try again". So it looks like JavaScript/AJAX is not supported at all on Windows Mobile without webkit. Please correct me if I'm wrong.

In another app using JQM, I included data-filter="true" in a listview. When built and run on Windows Mobile (without webkit), it graciously ignored that directive, and my app's functionality was not affected, albeit not having the filtering facility offered in other platforms.

So, on to some questions :

1. If we are to target WM without webkit, do we need to do all our validations in Ruby code (server-side)?

2. What JQM features (besides the data-filter attribute mentioned above) can be "safely used" to have a better UI  experience in other platforms, whilst at the same time not affecting the functionality of the WM-built app?

Jon Tara

unread,
Oct 11, 2012, 11:17:12 AM10/11/12
to rhom...@googlegroups.com

You should check the jQuery Mobile website and support forum.
Reply all
Reply to author
Forward
0 new messages