OOZOU
與我們聯繫
返回 TIL 列表
ruby on rails

用 Pundit Policy 管理 permitted parameters、預設值與各 action 的參數

February 10, 2022

要管理 permitted params,你可以在你的 policy 中加入 permitted_attributes 和 permitted_attributes_for_#{action},例如

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 railsJanuary 31, 2022

在 Turbo 中用 `requestSubmit` 送出表單

Turbo 會監聽 form 上的 submit 事件。因此,用 JS 送出表單時,請一律使用 requestSubmit。

ruby on railsJanuary 19, 2022

在 GitHub Actions 中讓 Rubocop 與 ESLint 忽略 bundled gems

Rails 專案在 GitHub actions 中通常會把 gems 安裝到 vendor 目錄,方便在後續執行時使用快取。

ruby on railsApril 18, 2021

Rails 6、Ruby 3、Bootstrap 5 與 Hotwire

以 Hotwire 延伸 Bootrails 的部落格文章: 所有最新潮的東西(截至我寫這篇 TIL 的時候):

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 代理人與生成式 AI
  • 行動應用開發
  • 數據分析與工程
  • UI/UX 與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策