惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
博客园_首页
量子位
T
Tailwind CSS Blog
博客园 - Franky
G
Google Developers Blog
D
DataBreaches.Net
Vercel News
Vercel News
B
Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
爱范儿
爱范儿
博客园 - 【当耐特】
The Cloudflare Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
C
Check Point Blog
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 5 設備器材篇 如何經營工程師 Youtube 頻道 - Part 4 後製剪輯篇 如何經營工程師 Youtube 頻道 - Part 3 文案企劃篇 如何經營工程師 Youtube 頻道 - Part 2 設備器材篇 如何經營工程師 Youtube 頻道 - Part 1 制訂頻道方向篇 如何經營工程師 Youtube 頻道 - Part 0 Rails 中避免 race condition 的最佳實踐(二) Rails 中避免 race condition 的最佳實踐(一) 10 分鐘整合 google sheet 做自動化開發功能週報 經營 Side Project 300 天所帶來的收穫及挑戰 我的 Youtube 影片製作流程 API 設計時必須注意的 HTTP header 底線問題 如何提升你的程式可讀性之實務技巧(三) 如何提升你的程式可讀性之實務技巧(二) 如何提升你的程式可讀性之實務技巧(一) Ruby 中使用 freeze 優化效能的時機 避免 React 中的 useEffect 無限 render 在 Rails 內輕量使用 Vue Component 的最佳實踐 如何在區域網路用 Docker 架設有 SSL 的 Gitlab 從被問到問人,那些我常問的面試問題 [Rails] 如何漂亮寫出可維護的 query (Maintainable Rails Query) 在已知長度情況下優化 slice 的性能
如何解決在 awesome print 中遇到 ActionController::Paramet...
Nic Lin · 2019-03-10 · via Nic Lin's Blog

raise unpermitted parameters error when try to awesome print ActionController::Parameters object

最近在自己的小專案裡面遇到這個問題,一下斷點看 params 就 raise error 給我。

會遇到這種情況的步驟是

  1. 你用了 Rails 5.1 以上的版本
  2. 你裝了 awesome_print 相關的套件,例如 awesome_rails_console
  3. 然後你在 controller 下了類似 byebugbinding.pry 能夠斷點的行為
  4. 你想在 console 輸出 params 看看傳了些什麼
  5. BOOM!!

You get some error

ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

主要問題是現在 ActionController::Parameters 不像以前是給 hash,而是給一個 object,這在 rails 5.1 後強制改掉了,詳見 PR

你可以試試在 console 裡面 reproduces

ap ActionController::Parameters.new

# ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

補充: ap 是 awesome_print 的 method

但如果換成 puts 就正常了…

puts ActionController::Parameters.new

# {}
# nil

合理推測 awesome_print 應該是會轉成 hash 來讓排版變好看?

追了一下 code,發現是在這裡被轉 hash 的,那恰巧碰上新的規定,在 unpermitted 前不能直接轉 hash 否則會 raise error 出來。

這個問題正在 awesome_print 開 PR 修復中,聽說要 merge 到 v2.0 之後 release

在此之前,如果要下斷點在 controller 又想看 params 的話,先用 params.to_unsafe_h 頂著吧 XD

或是乾脆移除這種排版的 GEM 也能根治。

參考資源