Ruby creating a simple logon window

Source: Internet
Author: User

First, we need a simple view for our index defined in Part 1

Index. rhtml in views/login

<SCRIPT type = "text/JavaScript"

Src = "/javascripts/login. js"> </SCRIPT>

 

<P> here comes the content which

Will be used after the user logged on. </P>

This is just some dummy content that will be "blocked" by a modal extjs window. there can be any HTML content inside it, which will be disabled, but wocould be visible via the HTML source code, so aditional client checks are neccessary if it shoshould work as a secure formular.

Now the interesting stuff is of course the login. js itself:

VaR loginform = new Ext. Form. formpanel ({

Basecls: 'x-plain ',

Labelwidth: 75,

URL: '/login/dologintest ',

Defaulttype: 'textfield ',

Items :[{

Fieldlabel: 'username ',

Name: 'name ',

Anchor: '200' // anchor width by percentage

},{

Fieldlabel: 'Password ',

Name: 'subobject ',

Anchor: '200' // anchor width by percentage

}],

Buttons :[{

Text: 'login ',

Handler: function (){

Loginform. getform (). Submit (

{

Method: 'get ',

Waitmsg: 'submitting ...',

 

Reset: false,

Success: function (){

Loginwindow. Close ();

 

},

Failure: function (Form, Action) {Ext. msg. Alert ('error', action. Result. Text )}

});

}

}]

 

});

 

VaR loginwindow = new Ext. Window ({

Title: 'login ',

Width: 300,

Height: 140,

Closable: false,

Minwidth: 300,

Minheight: 140,

Layout: 'fit ',

Plain: True,

Modal: True,

Bodystyle: 'padding: 5px ;',

Items: loginform

});

Ext. onready (function (){

Loginwindow. Show (this );

});

now lets get through this step by step:
the first function that is called is Ext. onready, which is the startup function that is called by the extjs toolkit after the page has finnished loading and the toolkit did initialize. the function shows up the loginwindow, which was declared earlier by the line "Var loginwindow = new Ext. window ({". parameters are always passed as a JavaScript Object {param1, param2, param3}, which might look a bit confusing at first, but is very practical to set different parameters. the real interesting Parmeter in loginwindow is the "items: loginform" line, it does define what will end up inside the window, here its a formpanel. the rest are basicly only stlye information to make it look like a window.

Now, the "loginform" object contains the real interesting stuff. you'll probably recognize the style stuff from the loginwindow object, along with the "items:" line. but here we are not referencing another object, but just inline the things we need as a JavaScript Object again, but this time as an array of objects, noted with the "items: [{object1}, {object2] "line. the "buttons:" line behaves the same, but uses button objects with onclick handlers instead.

Handler: function (){

Loginform. getform (). Submit (

{

Method: 'get ',

Waitmsg: 'submitting ...',

 

Reset: false,

Success: function (){

Loginwindow. Close ();

 

},

Failure: function (Form, Action) {Ext. msg. Alert ('error', action. Result. Text )}

});

}

This extends ctipt parts creates an Ajax submit form for the "login" button. extjs does take care of all houskeeping and wait messages, etc, you just need to tell what to display. the "success" and "failure" functions are user defined and send back from the Controller in form of an JSON hash. now the Controller looks like this:

Def dologintest

Headers ["Content-Type"] = "text/plain; charset = UTF-8"

 

Puts Params [: Name]

If (Params [: Name] = "mg ")

Data = {: Success => 'true '}

Else

Data = {: Failure => 'true',: text => "username or password wrong! "}

End

Render: text => data. to_json,: Layout => false

End

As you can see, its a very secure mechanic isim thats basicly foolproof and impossible to guess but all joking aside, you can see the result is gathered into a ruby hash or array, which is later then converted to JSON. for this to work you need either rails 2.0 (which is recommened because it makes life a lot easier with activerecord and JSON), or you need the JSON gem and put

Require 'json/objects'

on the top of the controller. using this mechanic IC you can transfer all kinds of data from your rails controller to the extjs frontend. in fact all communication between extjs and rails is basicly JSON, but this will be covered more in Part 3, where we'll have a more in deep look into getting some activerecord data into extjs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.