Productive Ruby on Rails
這陣子一直忙於專案.
之前的專案沒有使用Restful,於是寫起來有點辛苦,也不好整理.
這個專案一開始便使用Restful,配合Scaffold寫起來真的很快....
雖然,這是眾所皆知的東西了,但還是想紀錄一下.. :$
這是寫在Facebook上的文章,懶得重翻,將就一下囉.
英文不好請見諒. :p
I know why everybody say Ruby on Rails is productive now.
As most of parts, Ruby on Rails is there for you.
The only thing you need to do is just type some command and change some works.
For example. "Scaffold" is one of great tool that generate ready to go model for you.
In the one going project ,I scaffolded 4 times for all CRUD works.
Each scaffold for one set of CRUD, and it's ready with Restful style.
Only thing you need to do is build views and setting up some routes, some relationship between databases, and do a little bit work for your controller.
If you don't know what is Restful, and tried to understand how does scaffold works, it might drive you crazy.
Official docs guide you in pretty clear way : This
As others like iHower's PPT : This
And this : This
They all provide lots of information.
How easy if you go with Restful?
Sometimes,we want to create some relationship in this way : " find A user's POST "
In old way, you might create another action called "search post", and have lots of conditions in.
Maybe "find POST by user name", "find POST by comment".... as you can image, that will be lots of work to do.
In Restful way. you only need to set up relationship in routes like :
map.resources :users, :has_many=>{:posts,:comments}
then go into your each controller action "index" and add something like :
@post=find(:all,:conditions=>["user_id=?",params[:user_id]]) if params[:user_id]!=nil
then, when you type address like " users/1/posts/ " , it will show all posts by user_id 1 for u.
Cool, isn't it?
If you have further question, welcome discuss with me.

