简体   繁体   中英

How to add search term to Spotify API

I am building an app using rails, angular, and RSpotify . I'm still kind of a newbie, and feel like the answer to my question is right in front of me, but I am struggling to figure it out.

After I authenticate a user, I redirect to a search to find new songs/albums/artist etc. Once they find relevant songs, they can add them to a playlist.

Using RSpotify, I can make the data to json, and then show that data using angular. Here is an example of what I am doing;

users_controller.rb

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

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

I added to my routes, and now this spits out some data; localhost: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"
},

So, I am able to reach the data from spotify which is good. However, the call Artist.search takes a parameter. So my question is, using a search tab like this;

home.html.erb

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

How could I add user input from the search bar as the parameter for RSpotify::Artist.search() ? Any help pointing me in the right direction is very appreciated. Thanks.

users_controller.rb

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM