r5 - 05 Jun 2006 - 13:25:07 - CharinyaKlakhangYou are here: SETEC Wiki >  Knowledge Web  > WebTechnologyCategory > RubyOnRails > CodingOnRails > RubyOnRailsCourse1 > CreateAppOnRails

CreateAppOnRails

สร้าง controller

สามารถสร้าง controller โดยพิมพ์คำสั่งด้านล่างนี้

#C:\work\test>ruby script/generate controller Say

จาก command ดังกล่าวได้ทำการสร้าง controller ที่ชื่อว่า Say โดย rails จะสร้างไฟล์ที่เกี่ยวข้องกับการทำงานของ controller Say ขึ้น ใน source code นั้น จะมาดูไฟล์ controller ในโฟลเดอร์ app/controllers/say_controller.rb มีรูปแบบดังนี้

class SayController < ApplicationController
end

ทดสอบสร้าง action ชื่อ hello ใน say controller ในที่นี้ action นั้นจะทำหน้าที่ติดต่อกับ method ชื่อว่า hello ใน SayController? สามารถ เพิ่ม method ลงใน controller ได้ดังนี้

class SayController < ApplicationController
  def hello
  end
end

หน้าที่ของ controller คือ setup ค่าต่างๆ เพื่อที่จะแสดงผลผ่าน views ลอง run webrick เพื่อดูผลของการสร้าง controller ผ่านเว็บเบราเซอร์

จากภาพจะเห็นว่าเมื่อเรียก http://localhpst:3000/say/hello application มีการตอบสนอง แต่มีข้อความ error ปรากฎขึ้น สาเหตุเพราะ method hello นั้นพยายามติดต่อส่วนของ views โดยเรียก template ที่ชื่อว่า hello.rhtml ในโฟลเดอร์ /app/views/say/hello.rhtml ซึ่งเป็นไฟล์ที่มีไว้เพื่อแสดงผลลัพ์ของ method hello แต่เนื่องจากยังไม่ได้สร้างไฟล์ขึ้นมาจึงเกิด error ดังกล่าวขึ้น

สร้างไฟล์ hello.rhtml อย่างง่าย

hello.rhtml

<html>
    <head>
        <title>Rails Application</title>
    </head>
    <body>
        <h1>Welcome to Rails World !!</h1>
    </body>
</html>

Dynamic Page

สามารถทำได้ 2 วิธี 1. ใช้เทคโนโลยีที่เรียกว่า Builder 2. ใช้เทคโนโลยีที่เรียกว่า ERb (Embeded Ruby) เป็นการแทรก code ruby ลงใน template โดยไฟล์ template จะต้องมีนามสกุลเป็น .rhtml ERb จะทำหน้าที่แปลง .rhtml แสดงผลเป็น .html ในการแทรก code ruby นั้นจะใช้ แทรกภายใน tag <%= %> เป็น interpreted และตัว executed ของ ruby code ผลลัพธ์ที่ได้จากการ execute นั้นจะถูกแปลงเป็น string แล้วแสดงผลเป็น html ผ่านเบราเซอร์

แก้ไขไฟล์ hello.rhtml

<html>
    <head>
        <title>Rails Application</title>
    </head>
    <body>
        <h1>Welcome to Rails World !!</h1>
        <ul>
        <li>Say : <%= "Good " + "morning" %></li>
        <li>Say : <%= "Good " 'afternoon' %></li>
        <li>Day : <%= 3 + 4 %></li>
        <li>Month : <%= Time.now.month %></li>
        <li>Year : <%= Time.now.year %></li>
        </ul>
    </body>
</html>

เมื่อลอง refresh เบราเซอร์ จะได้ผลลัพธ์ดังนี้

นอกจากเราจะแก้ไข code ใน .rhtml template โดยตรงแล้วผู้พัฒนายังสามารถจัดการ set ค่าต่างๆ ผ่าน method ใน controller ได้ เพื่อให้ template นั้นมี code ที่ไม่ยุ่งยากเกินไปนัก โดยการสร้าง @time เป็นตัวแปรแบบ Instance variable

แก้ไขไฟล์ say_controller.rb

class SayController < ApplicationController
  def hello
    @time = Time.now
  end
end

แก้ไขไฟล์ hello.rhtml

<html>
    <head>
        <title>Rails Application</title>
    </head>
    <body>
        <h1>Welcome to Rails World !!</h1>
        <ul>
        <li>Say : <%= "Good " + "morning" %></li>
        <li>Say : <%= "Good " 'afternoon' %></li>
        <li>Day : <%= 3 + 4 %></li>
        <li>Month : <%= Time.now.month %></li>
        <li>Year : <%= Time.now.year %></li>
        <li>Time :<%= @time %></li>
        </ul>
    </body>
</html>

ผลลัพธ์ที่ได้คือ

สร้าง Link เชื่อมต่อระหว่าง page

ใน controller 1 controller นั้นสามารถสร้าง template ได้มากว่า 1 template ในที่นี้จะสร้าง template ใหม่เพิ่มขึ้นมา โดยจะทำการสร้าง method ชื่อ goodnight ใน say controller

แก้ไขไฟล์ say_controller.rb

class SayController < ApplicationController
  def hello
    @time = Time.now
  end
  def goodnight
  end
end

และไฟล์ใหม่ใน /app/views/say/goodnight.rhtml

<html>
    <head>
        <title>Rails Application</title>
    </head>
    <body>
        <h1>Welcome to Rails World !!</h1>
        <ul>
        <li>Say : <%= "Good " + "Night !!" %></li>
        <li>See you soon !!</li>
        <li>Time :<%= Time.now %></li>
        </ul>
        <p>
        <%= link_to "Go to Hello !!", :action => "hello" %>
    </body>
</html>

ผลลัพธ์คือ

แก้ไข /app/views/say/hello.rhtml เพื่อให้ link กับ goodnight.rhtml

<html>
    <head>
        <title>Rails Application</title>
    </head>
    <body>
        <h1>Welcome to Rails World !!</h1>
        <ul>
        <li>Say : <%= "Good " + "morning" %></li>
        <li>Say : <%= "Good " 'afternoon' %></li>
        <li>Day : <%= 3 + 4 %></li>
        <li>Month : <%= Time.now.month %></li>
        <li>Year : <%= Time.now.year %></li>
        <li>Time :<%= @time %></li>
        </ul>
        <p>
        <%= link_to "Go to Good Night !!", :action => "goodnight" %>
    </body>
</html>
Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r5 < r4 < r3 < r2 < r1 | More topic actions
 
Powered by SETEC Wiki
Copyright ©2012 by National Electronics and Computer Technology Center, NECTEC.
Ideas, requests, problems regarding SETEC Wiki? Send feedback