参数哈希键作为符号与字符串 [英] Params hash keys as symbols vs strings
本文介绍了参数哈希键作为符号与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%= params[:action] %>
和
<%= params['action'] %>
显示
index
但是这种语法有什么区别?
but what is the difference between this syntax?
推荐答案
在 Rails 中,params
哈希实际上是一个 HashWithIndifferentAccess
而不是标准的 ruby Hash
对象.这允许您使用像 'action'
这样的字符串或像 :action
这样的符号来访问内容.
In Rails, the params
hash is actually a HashWithIndifferentAccess
rather than a standard ruby Hash
object. This allows you to use either strings like 'action'
or symbols like :action
to access the contents.
无论您使用什么,您都将获得相同的结果,但请记住,这只适用于 HashWithIndifferentAccess
对象.
You will get the same results regardless of what you use, but keep in mind this only works on HashWithIndifferentAccess
objects.
这篇关于参数哈希键作为符号与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文