ruby: October 2006 Archives

Ruby Example

| | Comments (0) | TrackBacks (0)

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.

About this Archive

This page is a archive of entries in the ruby category from October 2006.

ruby: June 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.

ruby: October 2006: Monthly Archives

Powered by Movable Type 4.0