Is there a clean way to avoid calling a method on nil in a nested params hash?
hashmap, null, ruby, ruby-on-rails
Solution
Check Ick's maybe. You don't need to significantly refactor your code, just intersperse maybe proxies when necessary:
params[:subject].maybe[:name]
The same author (raganwald) also wrote andand, with the same idea.
Problem
I'm interested in getting the nested 'name' parameter of a params hash. Calling something like ``` params[:subject][:name] ``` throws an error when params[:subject] is empty. To avoid this error I usually write something like this: ``` if params[:subject] && params[:subject][:name] ``` Is there a cleaner way to implement this?