哈希:
p: {:headline=>"Managing Director at Test company name", :pid=>"0tSsRvCR7r", :first_name=>"John", :last_name=>"Doe", :industry=>"Financial Services", :summary=>nil, :public_profile_url=>"http://www.linkedin.com/pub/john-doe/7a/78/606", :distance=>0}
尝试调用 p.pid 但出现错误:
EXCEPTION: undefined method `pid' for #<Hash:0x007fcf1b3a29f0>
所有其他元素都可以正常访问。还为该字段尝试了不同的名称,但无济于事。任何人都可以对此有所了解吗?真的希望它不是那些你盯着看很久才意识到它很愚蠢的错误之一:/。
注意:我也尝试过 p['pid']。这也没有用。相对较新的 Rails。
最佳答案
尝试这样的事情:
p = {:headline=>"Managing Director at Test company name", :pid=>"0tSsRvCR7r", :first_name=>"John", :last_name=>"Doe", :industry=>"Financial Services", :summary=>nil, :public_profile_url=>"http://www.linkedin.com/pub/john-doe/7a/78/606", :distance=>0}
puts p
puts p[:pid]
关于ruby-on-rails - 访问哈希元素时未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18316384/