In my Rails app, I have the following method:
def navigation_menu(items)
# raise items.class.to_s
# raise items.to_yaml
render partial: 'global/navigation_menu', locals: items
end
If I uncomment the first line of the method, “Hash” is shown for the exception text, proving that items is a hash. If I uncomment the second line, the members of the hash are shown, so I also know it’s not empty.
That method fails with the following error:
comparison of String with :navigation_menu failed
If I replace locals: items with locals: { dummy_key: 'dummy value' }, it works.
Why can’t I use my items variable in place of an explicit hash?
What you wrote should work, I often proceed this way.
There is a particular case though: Hashes having keys as strings fail to be interpreted correctly. So it’s necessary in this case to append
symbolize_keysto the Hash: