About Me

My photo
PhD Candidate at Purdue University, Computer Science.

Thursday, March 29, 2007

Practical RESTful rails Example

Using PUT and DELETE with GET and POST is the main idea behind REST to simulate the CRUD mappings.
Note: Web-Browsers neither support PUT nor DELETE

Create a Rails project and the Resource Scaffolding
$ rails warehouse
warehouse> ruby script/generate scaffold_resource product name:string desc:text
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/products
exists test/functional/
exists test/unit/
create app/views/products/index.rhtml
create app/views/products/show.rhtml
create app/views/products/new.rhtml
create app/views/products/edit.rhtml
create app/views/layouts/products.rhtml
create public/stylesheets/scaffold.css
create app/models/product.rb
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/001_create_products.rb
route map.resources :products
warehouse> rake db:migrate
== CreateProducts: migrating ==================================================
-- create_table(:products, {:options=>"default charset=UTF8"})
-> 0.0160s
== CreateProducts: migrated (0.0160s) =========================================
We can now have a look to the generated controller.

URLs.
An example of URL in REST is (/product/1) instead of (/products/show/1). We notice that the action is dropped from the URL, we can't know what is the action that should happen to the addressed resource.
The REST actions are activated through a combination of resource-url and HTTP verb. Another feature in the REST action, is that it can react with different response-formats . REST uses the respond_to. We can see this in the generated controller we got from the above steps.
The format specification can be simply done by appending it to the request.(http://localhost:3000/products/1.xml)

Views
Remember the old days when we used to find the following line in the generated views:
link_to :controller => "products",:action=>"show", :id => product
>>>< href="/products/show/1">Show
Looking to our generated views, we see the following code instead:
<%= link_to 'Show', product_path(product) %>
>>>< href="/products/1">Show
The 'link_to' call is the same, but the hash is replaced by the path_methods.

Same thing for the following methods:
<%= link_to 'New product', new_product_path %>
>>>< href="/products/new">New product

<%= link_to 'Edit', edit_product_path(product) %>
>>>< href="/products/1;edit">Edit
It is not surprising to expect that the REST will affect our forms too. Now, we have to use Path-Methods in our Create/Edit forms.
The :url-hash is replaced with the calling of a Path-Method
  • product-path for the new-form.
  • product_path(:id) for the edit form.
<%= form_for(:product, :url => products_path) do |f| %>
>>>< action="/products" method="post">

<%= form_for(:product, :url => product_path(@product),
:html => { :method => :put }) do |f| %>
>>>< action="/products/1" method="post">
< style="margin: 0pt; padding: 0pt;">
< name="_method" value="put" type="hidden">
< / div>

Finally, the Destroy. The method used for both showing and deleting is 'project-path'. The only difference is that the destroy link additionally uses the parameter :method to name the HTTP method to use (:delete) because the browser doesn't support DELETE.
<%= link_to 'Show', product_path(product) %>
%= link_to 'Destroy', product_path(product),:method => :delete %>
In the same manner controllers have to take special care in using the new technique during redirect operation. Rails uses the URL methods generating a method for each path methos:
project_url for project_path
projects_url for projects_path
redirect_to :controller =>"products", :action=>"show", :id=>@project.id
>>>>
redirect_to project_url(@project)

Monday, March 26, 2007

RJS

Ruby on Rails has excellent support for Ajax baked right into the framework. Remote JavaScript (RJS) templates build upon the Ajax support offered by Rails, allow to easily update multiple page elements.
This is a quick start to work with RJS.
$ rails RJS_Example


Create your controller:

RJS_Example> ruby script/generate controller Examples
exists app/controllers/
exists app/helpers/
create app/views/examples
create test/functional/
create app/controllers/examples_controller.rb
create test/functional/examples_controller_test.rb
create app/helpers/examples_helper.rb


Let's modify the default controllers now:
class ExamplesController < ApplicationController  
def index
end
def display
@statement = params[:statement]
end
end


Now, we should go to create our views.
Create in the $RJS_Example/app/views/examples create your index.rhtml
In the header include this tag
    <%= javascript_include_tag :defaults %>

The :defaults helps to add all the Scriptaculous visual effects and controls.

In the body, add this code.
    <%= form_remote_tag :url => { :action => 'display' },
:html => { :id => 'display-form' } %>
<%= text_field_tag 'statement', nil, :size => 40 %>
<%= submit_tag 'submit statement' %>
<%= end_form_tag %>


We use the form_remote_tag() helper instead of the form_tag().
:html option helps to reset the form after completion.
:url option specifies the controller action that receives the form data.
Finally the empty "div id=statement",The id provides a way to reference the element from the RJS template.

Create partial template app/views/examples/_example.rhtml
  [<%= Time.now.to_s(:db) %>]

<%=h example %>


Create the RJS template: app/views/examples/display.rjs
page.insert_html :bottom, 'statements', :partial => 'example'
page.visual_effect :highlight, 'statements'
page.form.reset 'display-form'

The page object is an instance of the Rails JavaScriptGenerator.
the partial template app/views/examples/_example.rhtml is rendered, and the resulting content is inserted into the bottom of the statements div.

Sunday, March 25, 2007

Globalize on Ruby-On-Rails

The Globalize is an internationalization and localization plugin.
  • Translates both db content and view text.
  • Supports automatic selection of an alternate, localized template for each view.
  • Built-in localization and translation of strftime, numbers, and currency formats.
  • Supports pluralization.
  • All based on three automatically migrated database tables.
I is the best solution to support multi languages for your rails application.
The globalization wiki is not yet complete. But I think the "Get Started" part is fair enough to help you out making your globalized application, it provides very helpful external links.
  1. Sven’s Globalize Writeup – has many times been refered to as “the best documentation available for Globalize” .
  2. Example Application – takes you step by step through the process of creating a Globalized example application.
  3. Unit-tested Example – a walkthrough using a test-driven approach by Josh Harvey.

Saturday, March 24, 2007

My Visual DNA

Leadership User Guide

Leadership has becoming more and more important due to the globalization and its effect on the effective workforce in market.

Who is a leader?

A leader is someone who makes things happen by:

* Knowing the objectives and having a plan to achieve them.
* Building a team committed to achieving the objectives.
* Helping each member to give his best effort.

A leader must demonstrate two active traits: expertise and empathy. Also he takes active roles in remaking the environment in productive ways.

What is Leadership?

Leadership involves cooperation and collaboration activities that can occur only in a conductive context. It is the art of influencing a body of people to follow a certain course of action; the art of controlling them, directing them and getting the best out of them. A major part of leadership is man-management.
Leadership is the capacity to translate vision into reality.

There is a different between Management and Leadership.

Leadership Vs Management

Leadership

Management

People

Things

Empower

Control

Effectiveness

Efficiency

Principles

Techniques

Purpose

Method

Doing the right things

Do things right

Is the ladder against the right wall??

Climbing the ladder fast.

Leadership styles.

Creation of resonance can be done in six ways, leading to Six Leadership Styles. Typically, the most effective leaders can act according to and they can even skillfully switch between the various styles, depending on the situation.(According to Daniel Goleman).


Visionary Leadership

(Transformational Leadership)

Coaching Style

Affiliative Leadership

(People-Oriented)

Democratic Leadership

Pacesetting Leadership

(task oriented)

Commanding Leadership

How style builds resonance

He moves people towards shared dreams.

Connects what a person wants; with the organization's goals.

Creates harmony by connecting people to each other.

Appreciates people's input and gets commitment through participation.

Realizes challenging and exciting goals.

He decreases fear by giving clear direction in an emergency.

The impact of the style on the (business) climate

+ + +

+ +

+

+

Often ― ― when used too exclusively or poorly

Often ― ―

When style is appropriate

When changes require a new vision. Or when a clear direction is needed. Radical change.

To help competent, motivated employees to improve performance by building long-term capabilities.

To heal rifts in a team. To motivate during stressful times. Or to strengthen connections.

To build support or consensus. Or to get valuable input from employees.

To get high-quality results from a motivated and competent team. Sales.

In a grave crisis. Or with problem employees. To start an urgent organizational turnaround. Traditional military.

Saturday, March 17, 2007

Erlang Review

After I got a quick look to Erlang, I started to feel a little more curios to this language, and I decided to give it a portion of my readings.
The resources of this language are limited, it is very clear that it is still in the "Early Adopter" phase. I already believe that this language can realize a very good hype very soon. Furthermore, I see it has as many advantages as the Ruby.
I won't talk about the ordinary features which anybody can figure out by himself. What I see the most important point is "Concurrent Programming" in Erlang.
Now, I think of this blog entry as a checkpoint of my digging results in the Erlang World, and not an Erlang Quick Start. this is just an entry to point to the "Erlang Status".

Erlang is a programming language designed for building highly parallel, distributed, fault-tolerant systems. It has been used commercially for many years to build massive fault-tolerant systems which run for years with minimal failures. Erlang combines ideas from the world of functional programming with techniques for building fault-tolerant systems to make a powerful language for building the massively parallel networked applications of the future.
This means that your Erlang program should run X times faster on a X core processor than on a single core processor, without changing a single line in your code. I really appreciate this flexibility. You can use this feature in a very effective way in your application.
I didn't test the Erlang by myself and test its performance, but actually I am very optimistic towards the results, given that the Erlang is heavily used on embedded systems. so, I expect that it has a good performance comparing with other technologies such as Java, Ruby..etc
frankly speaking, I didn't test the Ruby on my PDA. But I expected that the performance won't be pleasant at all. so I decided to save my time for something else. About Java, yes well, I tested it and I am not ready to do it again.
I wondered if anybody used Erlang in web applications!!! The good news I found is that there is a plan to produce a web platform for Erlang. well, there is already a web platform for Erlang but it is not open source and information about it is very limited because it was developed outside of Ericsson
I think the Erlang consulting and Training Ltd has used an Erlang web platform. which I think a quite impressive start for the Erlang. When I think about Erlang Platform, I think of powerful concurrency handling of heavy loads (It is the Erlang nature, nothing to do with it), and I certainly remember the fault-tolerance embedded with Erlang (built-in ability to upgrade software during runtime without restarting or failure time) which is something missed in most of frameworks dominating the marketplace nowadays. This paper talks in some more details about the status of the Erlang Platform.

I hope you enjoy your Erlang programming.

Tuesday, March 13, 2007

Corporate Blogging

It's not about the way you spend your leisure time. Blogging or as we can call it here "Corporate Bloggings" is one of the technologies that have great potential impact in the marketplace.
The corporate-blogging is the use of online journals by employees to further company goals.

It started with nontechnology entries, but the lack of interest was a big factor of disappointments.
2005 was the peak of hype for corporate blogging. Currently it is sliding into the trough replaced by the (Social Network Analysis, Folksonomies, Speech Recognition for Mobile Devices, RSS Enterprise...)
The corporate-blog can be considered as a tool for projecting the marketing messages in a medium that has large reach and low cost. Furthermore, it creates a good opportunity to make some kind of dialogues with the market(collect feedback from the market).
Expectations are tricky in the blogging world. Although no initial efforts nor technical skills are needed for blogging, it is not easy to effectively achieve it because it requires long-term commitment and consistency.
Behind the scenes, you can expect the intelligent competition, and recruiting processes ;)

Saturday, March 10, 2007

From Consultance to Danger

From time to time, I try to remain connected to postgraduates classes. I find pleasure to meet people there and to refresh my head with academic tones.
Among the classes, I prefer the "Advanced Web Programming" the most. Sometimes I contribute as an audit!!! It may look strange, specially that I am a new born in the web technologies world.
The course is under supervision of two professors. One of them was going to chase me out, he thought that I represent a danger to the students. "Students knowing me well can rely on me as a friendly consultant and as a knowledge resource, they are going to be lazy" he thought so.
On the contrary, the other Professor thinks that I am a good guy, giving help to my colleagues, through my business oriented experience.
the third party is the students, they want me to come every Saturday to attend the lectures with them! some of them felt guilty while I was accused by the Professor.

I really like to help as much as I can, I can't stop this. in the same time I don't like lazy people.
but I have a theory about this...The students I deal with are not lazy, all wt I do is providing them the help through our conversations. When I figure out that who I am dealing with is not doing his best I start to to minimize my contribution with him.
I believe that we need to focus more on the keeping connection between different generations. Undergraduates should benefit from graduates experiences. but currently what happens is totally different; Only TAs are the ones involved with undergraduates.
Maybe I am wrong, maybe I am right. but I am convinced in what I am doing.

Friday, March 09, 2007

Quick look to Erlang

I started to be interested to the Erlang language. I read a little about it, and I think I may dedicate some time to it.
There is a short movie giving a quick introduction about ERLANG features.
Erlang has reached a high level of maturity in the telecoms market!!!!