# โหมด API ของ Rails (Ruby on Rails) > แอป API-only, การ render JSON, serializer, CORS, การยืนยันตัวตนด้วย token, การจัดการเวอร์ชัน - 20 คำถามสัมภาษณ์ - Mid-Level - [คำถามสัมภาษณ์: Ruby on Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions.md) ## 1. คำสั่งใดที่สร้างแอปพลิเคชัน Rails ใหม่ในโหมด API-only? **คำตอบ** แฟล็ก --api เมื่อสร้างแอปพลิเคชัน Rails จะสร้างแอปพลิเคชันขนาดเล็กที่ปรับให้เหมาะกับ API การกำหนดค่านี้จะตัดมิดเดิลแวร์ที่เกี่ยวข้องกับ view (cookies, sessions, flash) ออก ไม่รวม Action View และ Action Cable ตามค่าเริ่มต้น และกำหนดให้ ApplicationController สืบทอดจาก ActionController::API แทนที่จะเป็น ActionController::Base สิ่งนี้ช่วยลดขนาดแอปพลิเคชันและเพิ่มประสิทธิภาพสำหรับกรณีการใช้งานแบบ API ล้วน ๆ ## 2. ในแอปพลิเคชัน Rails API-only นั้น ApplicationController สืบทอดจากคลาสใด? **คำตอบ** ในแอปพลิเคชัน Rails API-only นั้น ApplicationController สืบทอดจาก ActionController::API แทนที่จะเป็น ActionController::Base คลาสนี้เป็นเวอร์ชันขนาดเล็กที่ไม่รวมโมดูลที่เกี่ยวข้องกับการ render HTML, sessions, cookies และ flash messages โดยจะคงไว้เฉพาะฟังก์ชันที่จำเป็นสำหรับ API เช่น routing, พารามิเตอร์, การ render JSON และ callback เท่านั้น สิ่งนี้ทำให้ได้ controller ที่มีประสิทธิภาพสูงขึ้นสำหรับ REST API ## 3. ควรใช้เมธอดใดในการ render การตอบกลับ JSON ใน Rails controller? **คำตอบ** เมธอด render พร้อมตัวเลือก json: เป็นวิธีมาตรฐานในการคืนค่า JSON ใน Rails โดยจะแปลงอ็อบเจกต์ที่ส่งเข้ามาเป็น JSON โดยอัตโนมัติผ่านเมธอด to_json คุณสามารถปรับแต่งรหัสสถานะ HTTP ด้วยตัวเลือก status:, ยกเว้นแอตทริบิวต์ หรือใช้ serializer เพื่อการควบคุมที่ละเอียดยิ่งขึ้น แนวทางนี้เรียบง่ายและเพียงพอสำหรับกรณีการใช้งานพื้นฐานส่วนใหญ่ ## มีอีก 17 คำถาม - gem ใดที่แนะนำให้ใช้สำหรับการจัดการ CORS (Cross-Origin Resource Sharing) ใน Rails API? - บทบาทของ serializer ใน Rails API คืออะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Ruby on Rails อื่นๆ - [พื้นฐาน Ruby](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/ruby-basics.md): 25 คำถาม, Junior - [การเขียนโปรแกรมเชิงวัตถุใน Ruby](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/ruby-oop.md): 20 คำถาม, Junior - [พื้นฐาน Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-fundamentals.md): 18 คำถาม, Junior - [Routing & Controllers](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/routing-controllers.md): 22 คำถาม, Junior - [พื้นฐาน ActiveRecord](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/active-record-basics.md): 25 คำถาม, Junior - [Views และ ERB Templates](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/views-erb-templates.md): 20 คำถาม, Junior - [การเชื่อมโยงของ ActiveRecord](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/active-record-associations.md): 24 คำถาม, Mid-Level - [การสอบถาม ActiveRecord ขั้นสูง](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/active-record-queries.md): 28 คำถาม, Mid-Level - [ฟอร์ม Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-forms.md): 20 คำถาม, Mid-Level - [การยืนยันตัวตนและการอนุญาต](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/authentication-authorization.md): 22 คำถาม, Mid-Level - [Asset Pipeline สมัยใหม่และ frontend](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-asset-pipeline.md): 18 คำถาม, Mid-Level - [การทดสอบด้วย RSpec](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-testing-rspec.md): 24 คำถาม, Mid-Level - [ActiveJob & Background Jobs](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/active-job-background-jobs.md): 20 คำถาม, Mid-Level - [ActionCable & WebSockets](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/action-cable-websockets.md): 18 คำถาม, Mid-Level - [ActionMailer](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/action-mailer.md): 18 คำถาม, Mid-Level - [ActiveStorage](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/active-storage.md): 20 คำถาม, Mid-Level - [กลยุทธ์การ Caching](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/caching-strategies.md): 20 คำถาม, Mid-Level - [การ Migration ขั้นสูง](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/database-migrations-advanced.md): 20 คำถาม, Mid-Level - [Rails Engines และแอปแบบโมดูลาร์](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-engines-modular-apps.md): 18 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/performance-optimization.md): 26 คำถาม, Senior - [Design Patterns ใน Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-design-patterns.md): 22 คำถาม, Senior - [เมตาโปรแกรมมิงใน Ruby](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/ruby-metaprogramming.md): 20 คำถาม, Senior - [ความปลอดภัยของ Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-security.md): 22 คำถาม, Senior - [GraphQL กับ Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/graphql-rails.md): 20 คำถาม, Senior - [การดีพลอยและโปรดักชัน](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-deployment-production.md): 20 คำถาม, Senior - [Monitoring & Logging](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/monitoring-logging.md): 20 คำถาม, Senior - [กลยุทธ์การอัปเกรด Rails](https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-upgrade-strategies.md): 18 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/ruby-on-rails/interview-questions/rails-api-mode