OOZOU
ติดต่อเรา
กลับไปหน้า TIL
ruby on rails

ใช้ Pundit Policy จัดการ permitted parameters ทั้งค่า default และแยกราย action

10 กุมภาพันธ์ 2565

ในการจัดการ permitted params คุณสามารถเพิ่ม permitted_attributes และ permitted_attributes_for_#{action} ใน policy ของคุณได้ เช่น

class CommentPolicy < ApplicationPolicy
  def permitted_attributes
    %i[body]
  end

  def permitted_attributes_for_create
    %i[body application_id]
  end
end

จากนั้นใน controller ของคุณ

class CommentController
  def create
    @comment = Comment.create(comment_attributes) # { body: 'body', application_id: 1 }
  end
 
  def update
    @comment = Comment.find(params[:id])
    @comment.update(comment_attributes) # { body: 'new body' }
  end
 
  private def comment_attributes
    permitted_attributes(Comment)
  end
end

เพิ่มเติมเกี่ยวกับ ruby on rails

ruby on rails31 มกราคม 2565

ใช้ `requestSubmit` สำหรับการ submit form ร่วมกับ Turbo

Turbo คอยฟัง event submit ที่เกิดขึ้นบน form ดังนั้นให้ใช้ requestSubmit เสมอเมื่อจะ submit form ด้วย JS

ruby on rails19 มกราคม 2565

ให้ Rubocop และ ESLint ignore gem ที่ bundle ไว้ใน GitHub Actions

โปรเจกต์ Rails มักติดตั้ง gem ลงใน vendor directory ใน GitHub actions เพื่อให้ cache ไว้ใช้ในรอบถัด ๆ ไปได้

ruby on rails18 เมษายน 2564

Rails 6, Ruby 3, Bootstrap 5 และ Hotwire

ต่อยอด blogpost ของ Bootrails ด้วย Hotwire: ของใหม่สุดเท่ทั้งหมด (ณ ตอนที่เขียน TIL นี้):

มีโปรเจกต์ในใจ?

เรายินดีรับฟังสิ่งที่คุณกำลังสร้าง

เริ่มต้นสนทนาhello@oozou.com
OOZOU Logo

กรุงเทพฯ · สิงคโปร์ · ฮ่องกง

X
Facebook
Instagram
LinkedIn

บริการ

  • พัฒนาเว็บ
  • เอเจนต์ AI และ Generative AI
  • พัฒนาแอปมือถือ
  • วิศวกรรมและวิเคราะห์ข้อมูล
  • UI/UX และการออกแบบผลิตภัณฑ์
  • การเปลี่ยนผ่านสู่ดิจิทัล

บริษัท

  • เกี่ยวกับเรา
  • ร่วมงานกับเรา
  • บล็อก
  • กรณีศึกษา
  • Today I Learned
  • ติดต่อเรา

เพิ่มเติม

  • อุตสาหกรรม
  • เครือข่ายพันธมิตร
© 2026 OOZOU สงวนลิขสิทธิ์
นโยบายความเป็นส่วนตัวจรรยาบรรณนโยบาย ABAC