October 2006 Archives
On my page is used prototype and scriptaculous lib.
The part I actually used, was an Effect. Effects can be performed on <div> tags for example, essential is an existing unique id-attribute. So the target for the effect looked like this:
<jsp:include flush="false" page="aPage.jsp"/>
The Effect functions should work on all so called block elements.
ADDRESS BLOCKQUOTE BODY DD DIV DL DT FIELDSET FORM, FRAME FRAMESET H1, H2, H3, H4, H5, H6 IFRAME NOFRAMES OBJECT P OL, UL, LI APPLET CENTER DIR HR MENU PRE
I called the effect in the onclick-event of an anchor element, passing the div-id as the parameter.
<a href=”http://www.innoq.com/blog/ca/wordpress/#” mcehref=”http://www.innoq.com/blog/ca/wordpress/#”onclick=”Effect.toggle(‘content’,’slide’); return false”> <img src=”../images/minimize.gif” mcesrc=”../images/minimize.gif” alt=”” /> </a>
All available effects can be found here.
Last week i had the chance to start my first [ruby (on rails)](http://www.rubyonrails.org/) experience. After downloading and installing and setting paths etc. i thought it would be great to compare creating a web app login in ruby with the one i´ve created in Jsf some weeks ago.
Creating a new ruby project on the command line was easy:
In the newly created folder "myfirstruby" ruby had created its project structure. I´ve only used "config" and "app" so far. In config i edit "database.yml" to connect to my local mysql database.
Database was already loaded with a table "Users" and columns "ID, NAME, PASSWORD". Next step in Ruby was to create a model class "User".
ruby script/generate model user
Strange behavior though in ruby: Singular named model "user", but tablename had to be plural "users".
Now an action is needed. A ruby controller serves as the action for one page or part of a page. It could also be generated:
ruby script/generate controller login
ruby script/generate controller user
Files "user_controller.rb" and "login_controller.rb" were in /app/controllers.
For the user controller I used the "scaffolding" concept of ruby. It adds hidden CRUD methods to the controller. They can be overwritten to implement custom behaviour. I just added to lines to "user_controller.rb":
A deployment to the server and a look at the site "localhost:3000/user/list" should bring a data table with actions. The server can be started by:
ruby script/server webrick
And now lets go and implement the login. First we need a login page. Pages in ruby have the ending "rhtml" and lie in "/app/views". My login view lies in "/app/views/login" and is named "index.rhtml":
<%= start_form_tag :action => 'login' %>
<%= text_field 'user', 'name' %>
<%= password_field 'user', 'password' %>
<%= submit_tag 'Login!' %>
The next place for editing is the "login_controller.rb". I added two methods: "index" and "login".
An index method/view will opened if there is no scaffolding used. The upcoming login method handles the request sent by the submit-tag.
if @session["user"]=User.authenticate (@params['user']['name'],@params['user']['password'])
redirect_to :controller => "user"
redirect_to :action => "index"
Notice the instance variable "params". This array always holds the parameter from the form. Here it has two dimensions, because of the naming conventions for the text_fields on the page. Logic is pretty simple. It uses a method of the user model and forwards to either the action or the controller. The "if" accepts only true values.
def self.authenticate(name, password)
find_first([ "name = '%s' AND password = '%s'", name, password ])
The self variable represents the object whose method is called. "find" or "find_first" are class methods which connect to the database and retrieve values.
Thats basically it ! There is no mapping to a standard welcome page, so you have to use "localhost:3000/login" to get to the login screen.