Ruby on Rails :: Validation

Posted by PunNeng, Sun Jul 16 17:45:00 UTC 2006

มาต่อกันดีกว่า

จากคราวที่แล้ว ถ้าหากว่าเราเกิดลืมใส่ title หรือ body มันจะดูไม่ดีแน่นอน หรือว่าใส่ title ซ้ำกันอีก อันนี้ก็ดูไม่ได้ เราเลยต้องมีการตรวจสอบความถูกต้องก่อนใส่ในฐานข้อมูล แล้วเราจะไปตรวจสอบในไหนดีล่ะ??? ก็ใน model ไง ตอนนี้ model จะทำหน้าที่เหมือนนายด่าน อะไรจะเข้าหรือออกจากฐานข้อมูล จะต้องผ่าน model ก่อนเสมอ งั้นเข้าไปใน model ดูเลยดีกว่า เข้าไปที่ app/models/post.rb กันเลย (เราสร้างไว้แล้วโดยใช้ scaffold จำได้เป่าเอ่ย)

class Post < ActiveRecord::Base
end

บ๋อแบ๋ ว่างเปล่า แล้วส่วน db mapping หรือส่วนอื่นๆ ไปไหนหมด มันอยู่ใน ActiveRecord นั่นเอง เราแค่ไป extends(<) คลาสแม่ เราก็จะทำอะไรได้ เหมือนๆ กับคลาสแม่แล้ว แล้วเพิ่มส่วนของการ validate ตามนี้

class Post < ActiveRecord::Base
    validates_presence_of :title, :body
end

'validates_presence_of' จะทำหน้าที่ตรวจสอบว่า field ที่เรากำหนดไว้ มีค่าว่างเปล่าหรือไม่ ถ้าว่าง มันจะส่ง error msg ไปให้หน้าเว็บ มาลองเล่นดู

แล้วเพิ่มบรรทัดนี้อีก

validates_uniqueness_of :title

ตัวนี้จะทำหน้าที่ตรวจสอบว่ามันซ้ำกันหรือไม่ ถ้าซ้ำก็ส่ง error กลับไปเลย

ต่อไป ก็มาจัดหน้าตาให้มันดูดีกว่านี้หน่อยดีกว่า ไปที่ app/views/admin/list.rhtml เลย ของเก่าเป็นไงไม่รู้ แต่ใช้อันนี้ดีกว่า

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
<h1>Listing posts</h1>
<%= link_to 'New post', :action => 'new' %>  
<table cellpadding="5" cellspacing="2" >
  <tr class="headTable">
    <th>Id
    <th width="40%">Post
    <th>Created at
    <th>Updated at
    <th>Actions
  </tr>
  <%
    odd_or_even = 0
    for post in @posts
      odd_or_even = 1 - odd_or_even
  %>
  <tr valign="top" class="listLine<%= odd_or_even %>">
  <td>
    <%= post.id %>
  </td>
  <td>
    <span class="listTitle">< %= h(post.title) %></span><br />
    <%= h(truncate(post.body,40)) %>
  </td>
  <td align="center">
    <%= post.created_at.strftime("%y-%m-%d") %><br />
  </td>
  <td align="center">
    <%= post.updated_at.strftime("%y-%m-%d") %><br />
  </td>
  <td class="listActions">
    <%= link_to 'Show', :action => 'show', :id => post %> | 
    <%= link_to 'Edit', :action => 'edit', :id => post %> | 
    <%= link_to 'Destroy', { :action => 'destroy', :id => post }, :confirm => 'Are you sure?', :post => true %>
  </td>
<% end %>
</tr></table>   
<%=  if @post_pages.current.previous
  link_to("Previous page", { :page => @post_pages.current.previous })
     end
%>
<%= if @post_pages.current.next
       link_to("Next page", { :page => @post_pages.current.next })
    end
%>

หลังจากที่เราส่ง @posts มาจาก controller ซึ่งมันจะเป็น array แล้วเราก็มาทำการวนลูป เอาแต่ละตัวใน @posts มาแสดง function ใหม่ๆ ที่เราเรียนรู้คือ

h(String) ไว้จัดการกับพวก < ,> อะไรพวกนี้ ให้เราสามารถมองเห็นได้ในหน้า html truncate(String,Fixnum) ไว้แสดง String ที่เราใส่เข้าไปใน parameter ตัวแรก เป็นจำนวนตัวที่เราใส่ไปใน parameter ตัวที่สอง

แล้วในส่วนข้างล่าง มีตัวคุมหน้าด้วย หน้าต่อไป หรือย้อนกลับ

แล้วเอา css ไปเพิ่มหน่อย ที่ public/stylesheets/scaffold.css

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
.headTable{
  background: #accdde;
}

.listTitle {
  color:       #244;
  font-weight: bold;
  font-size:   larger;
}
.listActions {
  font-size:    x-small;
  text-align:   right;
  padding-left: 1em;
}
.listLine0 {
  background: #dce7ed;
}
.listLine1 {
  background: #eeeeee;
}

คลิกเพื่อดูขนาดจริง

เริ่มดูดีแล้ว ไว้ต่อคราวหน้าครับ

ปล. สำหรับรายละเอียดต่างๆ สำหรับ function ต่างๆ ที่เอามาใช้ สามารถดูได้อย่างละเอียดที่ Rails API
ปอ. ตอนนี้ผมกำลังทะเลาะกับ CRUD ของ Rails และ CSS อยู่ อันต่อไปอาจจะล่าช้าไปนิดนึง

แก้ไขล่าสุด วันที่ 7 กรกฏาคม 2550 เวลา 23.45 น.

Filed Under: | Tags: howto ruby on rails validation

Comments

Have your say

A name is required. You may use HTML in your comments.




codegent: we're hiring