Ruby 程式語言 和 Ruby on Rails 入門課程 (KaLUG 社群技術分享工作坊 2009/9/20)

開始 Rails 課程前,可以先閱讀 A List apart 的Rails 入門介紹

下載投影片:Ruby 程式語言入門導覽Rails 簡介與入門RESTful Rails,以及最後的Ruby 生態圈一覽

過往課程

1. 安裝 Ruby on Rails 基本環境 (Ubuntu)

參考快速安裝 Rails 開發環境 Ubuntu 9.04這篇文章,執行安裝指令

2. 使用 irb 練習 Ruby 程式語言

  1. 練習 String 操作: API 參考
  2. 練習 Array 操作:API 參考
  3. 練習 Hash 操作:API參考

3. 建立一個 Hello World! Rails Project

  1. rails myproject
  2. cd myproject
  3. script/generate controller welcome
  4. 編輯 /app/controllers/welcome_controller.rb,加入
    		def say
    		 render :text => "Hello world!"
    		end
    	
  5. 瀏覽 http://localhost:3000/welcome/say,將看到 Hello world!
  6. 編輯 /app/controllers/welcome_controller.rb,加入
    		def index
    		end
    	
  7. 新增 /app/views/welcome/index.html.erb,內容是
    		<p>Hola!</p>
    		<p><%= link_to 'hello world', :controller => 'welcome', :action => 'say' %><p>
    	
  8. 瀏覽 http://localhost:3000/welcome/say,將看到 Hola! 及 hello 超連結。

3. 修改 Rails 的預設首頁

  1. 修改 /config/route.rb 加上 map.root :controller => "welcome"
  2. 刪除 /public/index.html

新增一個 Model 並使用 Console 練習

  1. script/generate model event name:string description:text
  2. rake db:migrate
  3. script/console

實做一個基本的 CRUD 應用程式

  1. script/generate controller events
  2. 編輯 /app/controllers/events_controller.rb 加入
      def index
        @events = Event.find(:all)
      end
    
      def show
        @event = Event.find(params[:id])
      end
      
      def new
        @event = Event.new
      end
      
      def create
        @event = Event.new(params[:event])
        @event.save
        
        redirect_to :action => :index
      end
      
      def edit
        @event = Event.find(params[:id])
      end
      
      def update
        @event = Event.find(params[:id])
        @event.update_attributes(params[:event])
        
        redirect_to :action => :show, :id => @event
      end
      
      def destroy
        @event = Event.find(params[:id])
        @event.destroy
        
        redirect_to :action => :index
      end
    		
  3. 新增 /app/views/events/index.html.erb,內容如下:
    <ul>
    <% @events.each do |event| %>
      <li>
        <%= link_to event.name, :controller => 'events', :action => 'show', :id => event %>
        <%= link_to 'edit', :controller => 'events', :action => 'edit', :id => event %>
        <%= link_to 'delete', :controller => 'events', :action => 'destroy', :id => event %>
      </li>
    <% end -%>
    </ul>
    <%= link_to 'new event', :controller => 'events', :action => 'new' %>
    	
  4. 新增 /app/views/events/show.html.erb,內容如下:
    <%=h @event.name %>
    <%=h @event.description %>
    
    <p><%= link_to 'back to index', :controller => 'events', :action => 'index' %></p>
    		
  5. 新增 /app/views/events/new.html.erb,內容如下:
    <% form_for @event, :url => { :controller => 'events', :action => 'create' } do |f| %>
        <%= f.label :name, "Name" %>
        <%= f.text_field :name %>
        
        <%= f.label :description, "Description" %>
        <%= f.text_area :description %>
        
        <%= f.submit "Create" %>
    <% end %>
    		
  6. 新增 /app/views/events/edit.html.erb,內容如下:
    <% form_for @event, :url => { :controller => 'events', :action => 'update', :id => @event } do |f| %>
        <%= f.label :name, "Name" %>
        <%= f.text_field :name %>
        
        <%= f.label :description, "Description" %>
        <%= f.text_area :description %>
        
        <%= f.submit "Update" %>
    <% end %>
    		
  7. 連往 http://localhost:3000/events

3-1. 加入 Layout

  1. 新增 /app/views/layout/application.html.erb,內容如下
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="content-type"
            
    content="text/html;charset=UTF-8" />
      <title>Posts: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold' %>
    </head>
    <body style="background: #EEEEEE;">

    <p style="color: green"><%= flash[:notice] %></p>

    <%= yield  %>

    </body>
    </html>

3-2. flash

  1. 在 create action 中加入 flash[:notice] = "新增成功"
  2. 在 update action 中加入 flash[:notice] = "修改成功"
  3. 在 destroy action 中加入 flash[:notice] = "刪除成功"

3-3. DRY: partial template

  1. 將 new.html.erb 和 edit.html.erb 的表單程式移往 _form.html.erb,內容如下
    	<%= f.label :name, "Name" %>
        <%= f.text_field :name %>
        
        <%= f.label :description, "Description" %>
        <%= f.text_area :description %>
        
    	
  2. 在 new.html.erb 和 edit.html.erb 的表單內加回
    			<%= render :partial => 'form', :locals => { :f => f } %>
    		

3-4. DRY: before_filter

  1. 在 events_controller.rb 中新增
     	before_filter :find_event, :only => [ :show, :edit, :update, :destroy]
     
  2. 新增函式如下:
     	protected
     	
     	def find_event
     		@event = Event.find(params[:id])
     	end 	
     
  3. 刪除 show, edit, update, destroy 函式中的 @event = Event.find(params[:id])

3-4. 加入 Model validation

  1. 編輯 /app/models/event.rb 加入
     		validates_presence_of :name
     	
  2. 編輯 new.html.erb 及 edit.html.erb 加入
      <%= error_messages_for :event %>
     

4. 修改成一個 RESTful 版本的 CRUD

  • 編輯 /config/routes.rb,加入
    map.resources :events
    	
  • 編輯 /app/views/events/index.html.erb,內容如下:
  • 編輯 /app/views/events/show.html.erb,內容如下:
    <%=h @event.name %>
    <%=h @event.description %>
    
    <p><%= link_to 'back to index', events_path %>

  • 編輯 /app/views/events/new.html.erb,內容如下:
    <% form_for @event, :url => events_path do |f| %>
        <%= f.label :name, "Name" %>
        <%= f.text_field :name %>
        
        <%= f.label :description, "Description" %>
        <%= f.text_area :description %>
        
        <%= f.submit "Create" %>
    <% end %>
    
  • 編輯 /app/views/events/edit.html.erb,內容如下:
    <% form_for @event, :url => event_path(@event), :html => { :method => :put } do |f| %>
        <%= f.label :name, "Name" %>
        <%= f.text_field :name %>
        
        <%= f.label :description, "Description" %>
        <%= f.text_area :description %>
        
        <%= f.submit "Update" %>
    <% end %>
    
  • 6. 使用 RESTful 版的 Scaffold

    1. script/generate scaffold person name:string bio:text birthday:datetime
    2. rake db:migrate
    3. Ctrl+C 關閉 Server,重新啟動 script/server
    4. 瀏覽 http://localhost:3000/people 並操作 CRUD

    7. Ajax 實做練習1 (傳回HTML更新)

    1. 編輯 /app/views/layouts/application.html.erb 加入
      	    <%= javascript_include_tag :defaults %>
          
    2. 編輯 /app/views/welcome/index.html.erb,加入
      	  
      <p><%= link_to_remote 'Ajax hello', :url => { :controller => 'welcome', :action => 'say' }, :update => 'foobar' %></p>
      
      
      <div id="foobar">
      </div>
      
    3. 瀏覽http://localhost:3000

    8. Ajax 實做練習2 (使用RJS)

    9. 使用者註冊登入 Generator 產生器

    10. 分頁 Plugin