Ruby on Rails
Why not we try Ruby as the whole world is suffering from Ruby fever. As I thought I had started playing (here I used word ‘play’ because I became fan of it) with Ruby on Rails.
Firstly I started reading one of four books named ‘Springer Ruby on Rails for PHP and Java Developers’. The book is so good and in fact perfect for me as I know PHP and Java.
There I found that once you install the Rails it will create and make your small application one-third done. OK let me share from the beginning what I did.
1. First of all I have downloaded the installer of Ruby from site for WIndows and installed it according to the instruction.
2. Then I have configured Rails.
3. Then I have created the database manually in mysql.
4. Then I created my project directory ‘c://ruby/myprojects’.
5. After that following the instruction I run my command prompt and by going to my project dir I run a command ‘rails myfirstpro’. Wow! It creates the complete directory structure of Rails!!
6. Then I entered to that directory using command prompt.
7. Then I have edited the ‘database.yml’ file in the ‘config’ directory.
development:
adapter: mysql
database: yourdatabase
username: yourusername
password: yourpassword
host: localhost
timeout: 5000
8. Then run another command ‘ruby script/generate scaffold user’ to create basic controller, model and views for a database table called users. WOW!! Now it has created model, controller and four views along with a layout.
9. Then you have to run a command in the command prompt ‘ruby script/server’ and keep it running untill you finish using your ruby application.
10. After that I found that the pages don’t show the data. That was beacause only the basic view pages were created and you have make the changes according to your database table fields.
11. Here is the sample how the index page was created:
<h1>Listing users</h1>
<table>
<% for user in @users %>
<tr>
<td><%= link_to ‘Show’, user %></td>
<td><%= link_to ‘Edit’, edit_user_path(user) %></td>
<td><%= link_to ‘Destroy’, user, :confirm => ‘Are you sure?’, :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to ‘New user’, new_user_path %>
But to view my data I had made some changes. And after that the file is like below:
<h1>Listing users</h1>
<table>
<tr class=”thead”>
<td><strong>Email</strong></td>
<td colspan=’3′><strong>Action</strong></td>
</tr>
<% for user in @users %>
<tr>
<td><%= user["email"]%> </td>
<td><%= link_to ‘Show’, user %></td>
<td><%= link_to ‘Edit’, edit_user_path(user) %></td>
<td><%= link_to ‘Destroy’, user, :confirm => ‘Are you sure?’, :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to ‘New user’, new_user_path %>
Here the red colored texts are added by me.
12. Now it shows the data. Here let me clear you one thing that is my database table name is users. And here is the following controller which was created:
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
# GET /users/new
# GET /users/new.xml
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = ‘User was successfully created.’
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => “new” }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
puts
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = ‘User was successfully updated.’
format.html { redirect_to(@user) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
And the model was created as following:
class User < ActiveRecord::Base
end
13. Then I found I am unable to use the add and edit form as their is no input box. Here is the following insert form.
The red colored code was added by me and then it worked nice.
<h1>New user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<label for=”user_email”>Email:</label><%= f.text_field :email, :live=> true %>
</p>
<p>
<label for=”user_password”>Password:</label>
<%= f.password_field :password, :live=> true %>
</p>
<p>
<%= f.submit :Create %>
</p>
<% end %>
<%= link_to ‘Back’, users_path %>
14. For validation I have added the following code in the mode:
class User < ActiveRecord::Base
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_presence_of :email, :password, :message => “Missing required field”
validates_length_of :password, :minimum=>5 ,:message => “Please provide password with minimum of 5 charecter”
validates_uniqueness_of :email, :message=> “already registered”
end
Then the form was a fully working with validation.
16. And then I have tried to use ajax validation and used live validation. And then added the red colored code in the form.
<h1>New user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<label for=”user_email”>Email:</label><%= f.text_field :email, :live=> true %>
<script type=”text/javascript”>
var user_email = new LiveValidation(’user_email’);
user_email.add( Validate.Email);
user_email.add( Validate.Presence );
</script>
</p>
<p>
<label for=”user_password”>Password:</label>
<%= f.password_field :password, :live=> true %>
<script type=”text/javascript”>
var user_password = new LiveValidation(’user_password’);
user_password.add( Validate.Presence);
user_password.add( Validate.Length, { minimum: 5, maximum: 20 });
</script>
</p>
<p>
<%= f.submit :Create %>
</p>
<% end %>
<%= link_to ‘Back’, users_path %>
and also added <%= javascript_include_tag ‘prototype’, ‘livevalidation_standalone’ %> to the layout header.
Now it worked with ajax validation.
Hurray! Now I know Ruby as well. And became a fan of it. I want to say like McDonanlds tv commercial “I’m lovin it”.

I am a simple man working in IT field. I love Music, playing soccer, traveling new places. By profession I am a software engineer and enjoy my job. Currently I am doing my M Sc in Software Engineering in Blekinge Institute of Technology, Sweden. I love researching on new technologies such as programming languages, architectures, tools and techniques.
Recent Comments