访问 Ruby 哈希变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16194219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Ruby hash variables
提问by Blaine Kasten
I am pretty new to ruby and sinatra but basically I have this route:
我对 ruby 和 sinatra 很陌生,但基本上我有这条路线:
put '/user_list/:user_id' do
puts request.params["model"]
end
and it returns the following
它返回以下内容
{"password":"36494092d7d5682666ac04f62d624141","username":"nicholas","user_id":106,"firstname":"Nicholas","email":"[email protected]","is_admin":0,"lastname":"Rose","privileges":""}
I am now having a hard time accessing values of each of those. It doesn't really seem to be in hash format so I can't really do
我现在很难访问每个值。它似乎不是散列格式,所以我真的做不到
request.params["model"][:password]
It just returns nil..
它只是返回零..
I just need to know what I can do to access those variables, or how to configure my request parameters to be in a good format to access variables.
我只需要知道我可以做什么来访问这些变量,或者如何将我的请求参数配置为访问变量的良好格式。
回答by Charles Caldwell
Try request.params["model"]["password"]
尝试 request.params["model"]["password"]
A Hash's keys can consist of both symbols and strings. However, a string key is different than a symbol key.
AHash的键可以由符号和字符串组成。但是,字符串键不同于符号键。
Note the following:
请注意以下事项:
h = {:name => 'Charles', "name" => 'Something else'}
h[:name] #=> 'Charles'
h["name"] #=> 'Something else'
EDIT:
编辑:
In your particular situation, it appears request.params["model"]returns a string instead of a hash. There is a method String#[]which is a means of getting a substring.
在您的特定情况下,它似乎request.params["model"]返回一个字符串而不是散列。有一种方法String#[]是获取子字符串的一种方法。
s = "Winter is coming"
s["Winter"] #=> "Winter"
s["Summer"] #=> nil
This would explain your comments.
这将解释您的评论。
There are a couple things you can do to remedy your specific situation. I have found the most simplest way to be using JSON. (I'm sure there are others and maybe those will surface through other answers or through comments.)
您可以采取一些措施来纠正您的具体情况。我找到了最简单的使用方法JSON。(我确定还有其他人,也许这些人会通过其他答案或评论浮出水面。)
require 'json'
hash_of_params = JSON.load(request.params["model"]).to_hash
hash_of_params["password"] #=> "36494092d7d5682666ac04f62d624141"
回答by Blaine Kasten
The standard Hash treats strings and symbols differently, and I'd be willing to bet that's what's happening in this case.
标准 Hash 以不同的方式处理字符串和符号,我愿意打赌这就是这种情况下发生的事情。
Use request.params["model"]["password"]to get the password.
使用request.params["model"]["password"]以获取密码。
The exception to that is when working with a HashWithIndifferentAccesswhich is part of ActiveSupport. For hashes of that type, either strings or symbols can be used to access the same elements.
例外情况是在使用HashWithIndifferentAccessActiveSupport 的一部分时。对于该类型的散列,可以使用字符串或符号来访问相同的元素。
回答by Arup Rakshit
Try the below,it will work too:
试试下面的方法,它也会起作用:
request.params["model"][:password.to_s]

