---
title: "ใช้ Pundit Policy จัดการ permitted parameters ทั้งค่า default และแยกราย action"
description: "จัดการ permitted params ได้ด้วยการเพิ่ม permitted\\attributes และ permitted\\attributes\\for\\#{action} ใน policy เช่น"
canonical: https://oozou.com/til/use-pundit-policies-to-manage-permitted-parameters-defaults-and-per-action-188
locale: th
date: 2022-02-10
topic: ruby-on-rails
---

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

> จัดการ permitted params ได้ด้วยการเพิ่ม permitted\attributes และ permitted\attributes\for\#{action} ใน policy เช่น

Published: 2022-02-10 · Topic: [ruby on rails](https://oozou.com/th/til-topics/ruby-on-rails) ([markdown](https://oozou.com/th/til-topics/ruby-on-rails.md))

จัดการ permitted params ได้ด้วยการเพิ่ม permitted\_attributes และ permitted\_attributes\_for\_#\{action} ใน policy เช่น

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

  def permitted_attributes_for_create
    %i[body application_id]
  end
end
```

จากนั้นใน controller

```ruby
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
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/th/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
