如何将搜索词添加到Spotify API

时间:2014-08-26 04:32:28

标签: ruby-on-rails json angularjs spotify

我正在使用rails,angular和RSpotify构建应用。我仍然是一个新手,觉得我的问题的答案就在我面前,但我正在努力解决这个问题。

在对用户进行身份验证后,我会重定向到搜索以查找新歌曲/专辑/艺术家等。一旦找到相关歌曲,他们就可以将它们添加到播放列表中。

使用RSpotify,我可以将数据转换为json,然后使用angular显示数据。这是我正在做的一个例子;

users_controller.rb

def music
        music = RSpotify::Artist.search("Wiz Khalifa")

        respond_to do |format|
            format.json { render :json => music}
        end
    end

我添加到我的路线中,现在这会吐出一些数据; 的本地主机:3000 / API / music.json

[
{
"genres": [],
"images": [
{
"height": 1200,
"url": "https://i.scdn.co/image/6fcf0a3556f9edf47f4d17c33a48418a3d2e6bce",
"width": 900
},
{
"height": 853,
"url": "https://i.scdn.co/image/d803a20e1402bf2c46353c97d929a3f28c27366e",
"width": 640
},
{
"height": 267,
"url": "https://i.scdn.co/image/0d706a776e505ad54968f389e4c6b820a7615562",
"width": 200
},
{
"height": 85,
"url": "https://i.scdn.co/image/534b45d0038ca0142b50dcc3f338a8fdd9caa949",
"width": 64
}
],
"name": "Wiz Khalifa",
"popularity": 91,
"top_tracks": {},
"external_urls": {
"spotify": "https://open.spotify.com/artist/137W8MRPWKqSmrBGDBFSop"
},
"href": "https://api.spotify.com/v1/artists/137W8MRPWKqSmrBGDBFSop",
"id": "137W8MRPWKqSmrBGDBFSop",
"type": "artist",
"uri": "spotify:artist:137W8MRPWKqSmrBGDBFSop"
},
{
"genres": [],
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/d4e1be09af57961ff630662daa5e44e75a99f18b",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/814960e7e3b52dc4b9985974e4b9f4e25e40860c",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/aa9543dd6a6fba6be98894c02141943da40cb81d",
"width": 64
}
],
"name": "Snoop Dogg & Wiz Khalifa",
"popularity": 34,
"top_tracks": {},
"external_urls": {
"spotify": "https://open.spotify.com/artist/3rnsFvKusYivCTnK2fpwbu"
},
"href": "https://api.spotify.com/v1/artists/3rnsFvKusYivCTnK2fpwbu",
"id": "3rnsFvKusYivCTnK2fpwbu",
"type": "artist",
"uri": "spotify:artist:3rnsFvKusYivCTnK2fpwbu"
},
{
"genres": [],
"images": [
{
"height": 600,
"url": "https://i.scdn.co/image/425f29c04ce8953339cac3b47e56dce53a82d396",
"width": 600
},
{
"height": 300,
"url": "https://i.scdn.co/image/7ea62fef2465095d3283843defd4daefb1a9b627",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/7f92197321b425d83b63ad3db550729fec74df9b",
"width": 64
}
],
"name": "BAyBOy Ft. Wiz Khalifa",
"popularity": 0,
"top_tracks": {},
"external_urls": {
"spotify": "https://open.spotify.com/artist/15qZ54HBDz6nDQKvafOKqG"
},
"href": "https://api.spotify.com/v1/artists/15qZ54HBDz6nDQKvafOKqG",
"id": "15qZ54HBDz6nDQKvafOKqG",
"type": "artist",
"uri": "spotify:artist:15qZ54HBDz6nDQKvafOKqG"
},

所以,我能够从spotify获得数据,这是好的。但是,调用Artist.search需要一个参数。所以我的问题是,使用像这样的搜索标签;

home.html.erb

<%= form_tag "/music" do %>
    <%= label_tag :search %>
    <%= text_field_tag :search %>
    <%= submit_tag :search %>
<% end %>

如何从搜索栏添加用户输入作为RSpotify::Artist.search()的参数?非常感谢任何帮助我指向正确方向的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

<强> users_controller.rb

def music
    @music = RSpotify::Artist.search(params[:search])
    render :json => @music
end