October 2006 Archives
Beside this i found another relatively new Java User Group in the Cologne Area.
With script.aculo.us javascript library it is absolutely easy to add nice visual gizmos to your page.
On my page is used prototype and scriptaculous lib.
<script src="../scripts/prototype.js" mcesrc="../scripts/prototype.js" type="text/javascript"></script>
<script src="../scripts/scriptaculous.js" mcesrc="../scripts/scriptaculous.js" type="text/javascript"></script>
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:
<div id="content">
<jsp:include flush="false" page="aPage.jsp"/>
</div>
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:
rails myfirstruby
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.
development:
adapter: mysql
database: devdb
username: myuser
password: mypass
host: localhost
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":
model :user
scaffold :user
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' %>
<p><label for="user_name">Name
<%= text_field 'user', 'name' %>
<p><label for="user_pass">Password
<%= password_field 'user', 'password' %>
<%= submit_tag 'Login!' %>
<%= end_form_tag
The next place for editing is the "login_controller.rb". I added two methods: "index" and "login".
def index
end
An index method/view will opened if there is no scaffolding used. The upcoming login method handles the request sent by the submit-tag.
def login
if @session["user"]=User.authenticate (@params['user']['name'],@params['user']['password'])
redirect_to :controller => "user"
else
redirect_to :action => "index"
end
end
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 ])
end
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.