1.ในการสร้างcrud โดยไม่ใ้ช่ scaffold
เราจะต้อง สร้าง model เช่น
ruby script/generate model student name:string surname:string
2.จากนั้นก็สร้าง controller
ruby script/generate controller students index new create update destroy edit show
3.จากนั้น ก็ไำปที่ config /routes.rb ไป map.resources :students
map.resources :students //students is a table name และ controller name ควรที่จะ ตรงกัน เนื่องจากหากมีการ route เกิดขึ้นซึ่งในที่ันี้ จะเป็น /students และ หากทำการ map.resources ดังกล่าว จะำได้ helper tag โดยจะมี path ที่ขึ้นต้นด้วย/students
<%form_for @student do|f |%>
<%=f.submit_tag "create/update"%>
<%end%>
Using Rails HelperTag for Access Resource
เนื่อง Rails มีการทำ resource mapping ซึ่งสามารถจัดการทรัำพยากร เช่น ตาราง students ใน database
โดยที่ ส่วนใหญ่แล้วเราก็ต้องมีบริการ พื้นฐานการจัดการ students เช่น การเพิ่ม จำนวน ลบ แสดงผล รายชื่อนักเรียน เป็นต้น เวลาที่เราจะไปใช้บริการดังกล่าวได้เราก็ต้อง route ให้ตรงกับบริการดังกล่าว(หลักๆแ้ล้วมี 7 ตัว)ซึ่งจะไปยัง controller ,action ต่างๆ ตามแปลความจาก routing path ดังนั้น rails จึงมีการสร้างการmap resources ไว้ใน route.rb ซึี่งเอาไว้สำหรับสร้างกฎการ route ให้กับ การบริการ resources ส่วนเรื่องรายละเอียดการบริการก็ต้องใช้action ใน controller เป็นตัวเชื่อมต่อกับ view เพื่อแสดงผลการบริการ
เช่น students_path ซึ่งเป็นไปได้ 2 กรณีคือ
ใช้กับ method 'GET' =>/resources
ใช้กับ method 'POST' => create
new_
ซึ่งเราสามารถเลือก method เป็น DELETE กับ PUT ได้
ถ้าีinstance มีค่า =>/students/id =>show
ถ้า instance ไม่มีค่า => /students =>index
ถ้ามีค่า ตามด้วย method "put" =>put ของ id นั้น//ปกติใช้ form for เนื่องจากต้องส่งค่า
ถ้ามีค่า ตามด้วย method "delete" =>delete ของ id นั้น
<%=link_to "delete" , resource_path(instance) ,:method =>:delete %>
edit_
เช่น edit_student_path(@s1)
ส่วน การสร้าง link ไป ยัง create , update จะต้องใช้ form_for เนื่องจาก มีการ ใช้ method "POST " กับ "PUT"เช่น
<%form_for@student do |f|%>
Name<%=f.text_field :name %>
<%end%>
โดยที่ จะเป็นcreate หรือ update ขึ้นอยู่กับว่า @student มีค่าหรืิอไม่ ถ้ามี ->update ไม่มี ก็ไปที่ create ดังนั้น
หาก @student =Student.new // ไป action =>create
@student =Student.find(1) //ไป aciton=>update
ถ้าไม่มีการสร้าง action create หรือ update ไว้ ก็จะฟ้อง
ปล. <%=link_to %> กับ <%=button_to %> จะต่างกันที่ button_to จะมีไม่มีการ link ไปยัง path ที่ใส่ไว้ แต่เปลี่ยน แค่ URI เท่านั้น
เราต้องทำการ map resource ก่อน จึงจะสามารถใช้ helper tagได้
ถ้าเราำไม้่มีการ map resource ไว้ แต่เราก็สามารถ อ้างอิงได้ แต่เป็นไปตาม กฎของ route ที่มีอยู่้เช่น
<%= link_to "Back" , :controller =>:students,:action=>:index %>
ปล.ถ้าเข้าใจ concept แล้วให้ใช้ generate scaffold ไปเลย ซึ่งจะสามารถสร้างที่กล่าวมาได้ทั้งหมด
Method for managing in database
find(:id) // find(1)
find :all ,:limit,:conditions,:read_only,:order,:form
ex. find :all ,:select=>"id,name",
:limit=>3,
:conditions=>":name='benz' AND :surname='narak' ",
:form=>"tables WHERE dept='cs' ",
:order=>"name DESC",
:read_only=>true
find_by_* //find_by_name
find_by_sql "_______"
>>Student.methods
Student.create :name =>" ",:surname=>""
Student.new(params[:students])
Method for instance
student.save
student.delete
student.destroy
student.update_attributes :name , "benz"
ไม่มีความคิดเห็น:
แสดงความคิดเห็น