วันเสาร์ที่ 26 มิถุนายน พ.ศ. 2553

define Attribute value

Define Attributes => list ของattribute ที่มีความสัมพันธ์กับตัวdata model class เนื่องจาก rails ช่วยในเรื่องของการ แปลงจาก data model concept ไปยัง table ใน database ซึ่งในเวลา runtime Active Record model จะืำทำการตรวจสอบ schema ซึ่งมี attribute ตามที่เราำได้ ทำการ migration ไว้
ซึี่้งเราอาจจะไม่จำเป็นต้อง extend จาก ActionRecord::Base ก็ได้
Default Attribute Values
ให้ที่ data model ซึ่งบาง attribute นั้น จำเป็นต้อง มีค่าเริ่มต้น เช่น score อะไรประมาณนี้ ซึ่งเป็นในส่วนของ data model เช่น
class Person ActiveRecord::base
def position
@position or 'n/a'
end
end

def position
read_attribute(:position) or 'n/a'
end

def position=(txt)
write_attribute(:position,txt+".")
end

p=Person.create
....... Position=>nil
>>p.position
employee


Hash Notation
c=Client.create :name=>"benz"
c[:name]

Attributes method
client.attributes
จะให้ attribute ที่่มีการกำหนดไว้

Accessing and Manipulating Attribute ก่อนการ TypeCast
activeRecord connection adapter จะ fetch ผลลัพธ์ของ String แล้ว railsจะ convert เป็น datatype ขึ้นอยู่กับ type ใน database column เช่น integer -> Fixnum class
class Client /< ActiveRecord::Base
before_save :string_checking
def string_checking
_before_type_cast.tr!('$,','')
end
end
จะทำการแทนที่เครื่องหมาย $กับ , ด้วย '' ก่อนที่จะมีการทำtype casting สำหรับ นั้นๆ
Reloading method
c=Client.create :name="benz"
c.reload
c.new_record?
->false
c=Client.new(:name=>"benz")
c.new_record?
->true
Dynamic Attribute-Based finder
คือการquery ด้วยการใช้คำสั่ง find ซึ่งจะทำการ execute เมื่อมีการเรียก method ที่ได้มีการdefineเอาไว้แล้ว
find_by_
find_all_by_ ซึ่งอาจจะได้ค่าออกมาเป็น อาร์เรย์หรือ single value ก็ได้
เช่น
find(:first,["user_name=?"AND password=?,user_name,password])
find_by_user_name_and_password
find_or_initialize_by_
param="benz"
find(:all ,conditions=>["name like ?",param])
Custom SQL QUERY ใช้ในยามจำเป็นเท่านั้น

วันพุธที่ 9 มิถุนายน พ.ศ. 2553

The Rails way:chapter 4

เหตุผลที่สำคัญที่ เหล่าำพวกพัฒนาเว็บ ทั้งหลายต้องเผชิญกันก็คือ
การพิจารณาการตั้งชื่อและการจัดการ roucesและ actionใน application
REST ป็นข้อกำหนดที่กำหนดขึ้นเพื่อการกระทำร่วมกันระหว่างองค์ประกอบของระบบ
ข้อกำหนดของ RESTได้แก่
-การใช้ client -server architecture
-Stateless communication เว็บsever ส่งข้อมูลกลับมาที่ client แล้ว การเชื่อมต่อจะถูกปิดทันที (ที่ตรงกัน ข้ามก็คือ statefull => เมื่อserver ส่งข้อมูลกลับมาให้client การเชื่อมต่อยังไม่ถูกปิด แต่จะค้างไว้จนกว่าจะมีการสั่งให้ปิด connection)
-Explicit signaling of response cacheability อันนี้ไม่รุคือไร

REST ถูกdesign มาเพื่อ ช่วยในการให้บริการโดยการใช้native Idiom หรือ โครงสร้างของ Http
สิ่งที่เป็นหัวใจของ REST ที่ support ใน Rails ก็คือ เทคนิคที่ใช้ในการname สำหรับ การ route
โดยอัตโนมัติ กล่าวคือ เราสามารถสร้าง name route ที่สามารถระบุname เป็น resource และสร้าง route ต่างๆที่เชื่อมไปยังการ managing resource(CRUD)นั้น ผ่านทาง action ในcontrollerทำได้โดย
map.resources :profiles

Singular and Plural Restful Route
-singular ในrouteที่ไปยังaction show new edit และ destroy ซึ่งเราจะทำบน particular resource
-plural ในroute ที่ำำเกี่ยวข้องกับกลุ่มของ resource

Singular Resource Routes
คือเลือกเอาเฉพาะ routeที่เป็น singular เนื่องจากเราต้องการแค่เพียงsingularโดยที่ในfile config/routes.rb ต้องเพิ่ม

map.resource :student

Nested Resources
ใช้สำหรับresource ที่มีมีความเกี่ยวข้องกันกับ อีกresourceหนึ่งเช่นการเสนอราคานั้นอยู่ในส่วนหนึ่งการประมูล หากเราต้องการเก็บข้อมูลเกี่ยวกับ การประมูลเช่นการประมูลครั้งที่ 1 นั้นมีการเสนอราคาเท่าใดบ้าง
เป็นต้น
auction/1/bids/5

ให้เรานึุกถึงความสัมพันธ์ีระหว่าง การสร้าง blog ที่ user ต้อง login เข้าไปเป็น authorก่อน จากนั้นจึงให้ authorนั้น สร้างบทควา่มได้ โดยที่ author แต่ละคนก็สามารถเขียนบทความได้หลายบทความ หากเราต้องการเข้าไปที่ user ที่มี id =1 บทความที่มี id เป็น 2สามารถทำได้โดย
author/1/article/2
โดยที่หากเราต้องการlink ไปยังหน้า index ของarticle ที่มี author_id=1 สามารถทำได้ โดย
<%=link_to "all article" ,author_articles_path(@author)%>
โดย ที่ @author เป็น instance ที่มี id=1 เราสามารถใส่ทั้ง instance เลย มันจะใส่ idให้อัตโนมัติ

หรือ ถ้าต้องการไปยังหน้า show
<%=link_to "show " ,author_article_path(@author,@article)%>

มีด้วยกัน 2 เหตุผลที่มีการสร้าง Nested Resourceขึ้น
-ความหมายของ Url จะสามารถบอกความหมายบางอย่างเกี่ยวกับ resource แก่ user ได้
-การ routeแบบนี้จะสามารถช่วยให้เราสามารถเข้าถึง auction ได้ง่ายผ่านทาง params[:auction_id] ได้เลยเนื่้องจากการrouteแบบนี้ จะมีการส่ง auction_id ไปตาม request ด้วย

map.resource :auction do|auction|
auction.resource :bids
end

Setting path Prefix
เราสามารถทำการ nested route ได้อีกวิธีหนึ่ง โดยการระบุ :path_prefix เช่น
map.resources :authors
map.resources :articles ,:path_prefix =>"authors/:author_id"

ซึ่งความแตกต่าง ของ 2เทคนิคนี้คือ
nested routes จะต้องแก้ไขในการอ้างอิงpath เนื่องจากจะมีการสร้าง helper tag ที่มีความสัมพันธ์ระหว่างกัน ขึ้นมา เช่น

author_articles_path เป็นต้น

ส่วน การใส่ path Prefix แล้วทำให้เป็น nested route จะแก้ไขง่ายกว่า เป็นการกำหนด routeว่าจะเรียก
articleได้จะต้องมี path_prefix ก่อน เนื่องจากเปลี่ยนที่ route
หากเราใช้

<%=link_to "click_to course " , courses_path ,:method=>"get" %>
จะลิ้งค์ไปยัง /students/:student_id/courses
ต่างจาก nested route
<%=link_to "click_to course" ,student_courses_path,:method =>"get"%>

ก็จะลิ้งค์ไปยัง /students/:student_id/courses


โดยที่เราสามารถตรวจสอบได้จาก rake routes>tmp.txt

the Rails way :chapter3

Routing
the two purpose of Routing
- Recognizing URLs -> เพื่อที่จะให้appliction ตัดสินใจว่าจะทำอะไรเมื่อมี request เข้ามา
-Generating URLs ->เนื่องจากได้มีการให้ใช้ high-level syntax ใน view template ดังนั้นมันก็จะ generate Urls path ในการ link ด้วย html ให้
ตัวอย่าง sample route

map.connect 'student/:profile',:controller=>"student",:action=>"show"

static string คือ student (ช่วยให่้จำprocess ได้ )
wildcard url component คือ :profile จะเป็นเหมือนตัวรับค่าโดยจะรับค่าอะไรนั้นขึ้นอยู่กับตำแหน่งที่ให้ค่าใน string pattern ซึ่งจะรับค่ามาจาก request url โดยทีี่่เราจะ accessค่านั้นได้จาก ในaction ใน controller โดยผ่านhash ที่ส่งมาตามrequest มายังaction เช่น request url คือ http://localhost:
3000/student/benz ดังนั้น params[:profile] ก็จะมีค่าิำ'benz'
bound parameter คือ(:controller =>"student", :action =>"show") เป็นตัวที่จะตัดสินใจว่า เมื่อมี request Urlมา จะให้ไปไหน
ก็คือ เมื่อมี request มา มันก็จะมาเช็คว่าตรงกับpattern string หรือป่าว ถ้าตรงจะไปยัง action ใน controller ตามที่map ไว้

Default route

map.connect ':controller/:action/:id'
โดยปกติแล้วเวลาเราค้นข้อมูลจาก database เราก็จะ:id ของมันมาแล้วส่งไปตาม request ไปยัง controller เพื่อ นำ id ไปอ้างอิงต่อได้

Default route generation

The Ante default route and respond_to
map.connect ':controller/:action/:id/:format' จะมีการรับ format เข้ามาด้วยซึ่งเราสามารถใช้ respond_to ในการเขียนaction ที่ให้ผลลัพธ์ที่แตกต่างกันได้ขึ้นอยู่กับ request format (HTML or XML)

The Empty Route
หน้าแรก
map.root :controller =>"homepage"
แต่ต้องลบ index .htmlใน /public ออกก่อน

the custom Route
หน้า ที่ requestไม่match กับ route

Route Order
มีผลต่อการmatching Route ถ้ามีการสลับตำแหน่งของ bound_parameter หรือ wildcard ก็จะไป
Using Literal Url
<%=link_to "help","/main/help"%>

Route Globbing
เมื่้อเราต้องการให้request ไปยังaction ที่เราต้องการแล้วให้ action นั้นจัดการกับrequest ที่เหลือ
เช่น
โดยเราต้องสร้างaction list ในcontroller ก่อน
จากนั้น ทำการ
map.connect 'contry/list/*address'
ในcontroller list
def list
@list= params[:address]
end
เราก็จะได้ @listเข้ามาจัดการได้

ิื์Named Route
name_url ก็ใส่ :action,:controller ไปเลย
name_path เป็นกาีร mapไว้ แล้วอ้างอิงตามรูปแบบที่กำหนด
เช่น
map.connect 'profile/:id '
,:controller =>"profile"
,:action=>"show"
หรือ
map.auction 'auction/:auction_id/item/:id',:controller => "auction",:action=
>"show"
ดังนั้นเราสามารถอ้าง pathใน view templated ได้เช่น
<%=link_to profile_path(profile.id)%>
profile.id จะใส่ เป็น instace ก็ำได้ เนื่องจากมันจะแปลงเป็น id ให้อัตโนมัติ
หรือ
<%=link_to "auction",auction_path(@auction,item)%>

special scope method
คือการรวมกลุ่มที่ใช้ bound parameter หรือ option บางอย่างร่วมกันเช่น
map.with_option :controller =>"main" do |main|
main.help '/help',:action=>"help"
main.contact '/contact' ,:action=>"contact"
end
ปลใช้controller เหมืิอนกัน

วันพุธที่ 2 มิถุนายน พ.ศ. 2553

The Rails way part I: install Rmagick

download ไอบ้านี่มา"rmagick-2.12.0-x86-mswin32.gem"
$gem install rmagick-2.12.0-x86-mswin32.gem





จากนั้นทดสอบการใช้งานดังนั้น
rails image
cd image
ruby script/generate controller pic index
ใน PicController

Render vs Redirect_to

render vs redirect_to in Rails
เนื่องจากมีcaseหนึ่งซึ่งพอจะอธิบายความแตกต่างระหว่าง render กับ redirect_to ได้ พอสรุปได้ดังนี้
ขออธิบายก่อนแล้วกันค่อยดูตัวอย่าง
ถ้าเป็น render =>เราจะไป render view template ภายใน หน้า .rhtml นั้นเลย โดยไม่มีการผ่าน actionใน controller นั้นก่อน แต่ถ้าเป็น redirect_to จะเหมือนกับเรามีการส่งrequest ใหม่ๆเลย มันก็จะไปยัง action ในcontroller แล้ว ก็จะไปrender view template นั้นอีกที ถ้าถามว่าผลลัพธ์มันต่างกันไหม ? บอกเลยว่าต่างมาก เนื่องจากมันไม่ผ่านaction ในcontrollerก่อน อยู่ๆก็ไป render view templateเลย หากเรามีการประกาศตัวแปรในcontroller แล้วแสดงผลใน viewก็จะำำไม่แสดง
ตัวอย่างเช่น
def index
render :action=>:test
end

def test
@test="hello world"
end

ใน test.rhtml
-----------------------------------------
<%=@test %>

ถ้าเป็น render จะำไม่แสดงผลออกเนื่องจากไม่ได้ execute

ถ้าเป็น redirect_to จะแสดงค่าออกมา คือ "hello world"
ลองดู
def index
redirect_to :action =>:test
end
----------
จะได้ hello world