[英]Can't access value of key in hash
I'm using the Rspotify gem (it's a wrapper for the Spotify API), and I'm trying to access the url of the first image in the below array: 我正在使用Rspotify gem (它是Spotify API的包装器),并且正在尝试访问以下数组中第一张图片的网址:
[# < RSpotify::Artist: 0x007f8558147050 @images = [{
"height" => 640, "url" => "https://i.scdn.co/image/cb080366dc8af1fe4dc90c4b9959794794884c66", "width" => 640
}, {
"height" => 320, "url" => "https://i.scdn.co/image/6bd672a0f33705eda4b543c304c21a152f393291", "width" => 320
}, {
"height" => 160, "url" => "https://i.scdn.co/image/085ae2e76f402468fe9982851b51cf876e4f20fe", "width" => 160
}]
]
I'm able to get to the following using artist.images.first
, but I can't access the URL in it by chaining anything (eg .url
, ["url"]
, ("url")
, [:url]
): 我可以使用artist.images.first
进行以下操作,但是我无法通过链接任何内容(例如.url
, ["url"]
, ("url")
, [:url]
("url")
访问其中的URL ):
{"height"=>640, "url"=>"https://i.scdn.co/image/cb080366dc8af1fe4dc90c4b9959794794884c66", "width"=>640}
Here's my controller: 这是我的控制器:
def search
unless params[:artist][:artist_name].blank?
artist = params[:artist][:artist_name]
@artists = RSpotify::Artist.search(artist)
render 'pages/profile'
end
end
Here's my view: 这是我的看法:
<h1> Search Results </h1>
<ul>
<% @artists.each do |artist| %>
<li> <%= artist.images.first['url'] %> | <%= artist.name %> </li>
<% end %>
</ul>
Using artist.images.first['url'] should solve it. 使用artist.images.first ['url']应该可以解决它。
The following code worked for me: 以下代码为我工作:
require 'rspotify'
artist = RSpotify::Artist.search('Arctic Monkeys').first
puts artist.images.first['url']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.