Ruby Example
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.
Categories
ruby0 TrackBacks
Listed below are links to blogs that reference this entry: Ruby Example.
TrackBack URL for this entry: http://www.innoq.com/mt4/mt-tb.cgi/2461
Leave a comment