Setting up a Ruby on Rails Project
Installing Rails
ติดตั้ง Rails ลงบนเครื่องโดยใช้คำสั่ง
สังเกตว่าเราจะมีคำสั่ง mise exec ruby -- นำหน้าอยู่ตลอดเวลา เพื่อใช้คำสั่งผ่าน mise ทีนี้เราสามารถละคำสั่งได้โดยการรัน
หรือเอาคำสั่งด้านบนนี้ไปใช้ไว้ใน Configuration ของ Shell ของเรา (เช่นไฟล์ ~/.zshrc เป็นต้น)
ปล. จากนี้ไปจะละคำสั่ง mise exec ruby -- ไว้ในฐานที่เข้าใจ
Creating a New Rails Project
Scaffolding
หรือเราจะย่อคำว่า generate ให้เหลือ g แทน
The name of the scaffold follows the convention of models, which are singular, rather than resources and controllers, which are plural. Thus, we have User instead of Users.
เวลาสร้าง controller ให้ใช้ plural เช่น จาก User ก็เป็น Users
Scaffolding is a temporary structure used to support a work crew to aid in the construction, maintenance and repair of buildings, bridges and all other man-made structures. – Wikipedia
— Hartl, Michael. Ruby on Rails Tutorial (p. 72). Pearson Education. Kindle Edition.
เวลาสร้าง Scaffold มาจะมีไฟล์ Controller, Model และ View มาให้ รวมไปถึงการมี Route ใหม่ และไฟล์ Migration มาให้เลย
จากนั้นให้เราสั่ง
ซึ่งถ้าเรามีการแก้หรือมีโค้ดเกี่ยวกับ Database เราจะใช้คำสั่งข้างต้นนี้เสมอ
ให้ลองเข้าหน้า http://127.0.0.1:3000 ดู
Debugging with Rails Console
สั่ง
หรือ
แล้วเราก็สามารถใช้ Methods ต่าง ๆ จาก Active Record ได้เลย สามารถดูรายละเอียดได้ที่ Retrieving Objects from the Database