---
title: "Punditポリシーで許可パラメータをデフォルトとアクション別に管理する"
description: "許可パラメータを管理するには、ポリシーに permitted\\attributes と permitted\\attributes\\for\\#{action} を追加します。例："
canonical: https://oozou.com/til/use-pundit-policies-to-manage-permitted-parameters-defaults-and-per-action-188
locale: ja
date: 2022-02-10
topic: ruby-on-rails
---

# Punditポリシーで許可パラメータをデフォルトとアクション別に管理する

> 許可パラメータを管理するには、ポリシーに permitted\attributes と permitted\attributes\for\#{action} を追加します。例：

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

許可パラメータを管理するには、ポリシーに permitted\_attributes と permitted\_attributes\_for\_#\{action} を追加します。例：

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

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

そしてコントローラーでは次のようにします

```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/ja/contact

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