guilhermesad / rspotify

A ruby wrapper for the Spotify Web API
MIT License
717 stars 290 forks source link

Add tracks to collaborative playlist #197

Closed rcamara-nq closed 5 years ago

rcamara-nq commented 5 years ago

I have a collaborative playlist and I want everybody that enters in my website could add a new track.

Search tracks and search for the playlist is already done but when I'm trying to add tracks to this playlist I get this error: uninitialized class variable @@users_credentials in RSpotify::User

My code looks like this:

class SiteController < ApplicationController
  before_action :authenticate, only: %i[search add_song]

  def search
    tracks = RSpotify::Track.search(params[:song])

    render json: tracks
  end

  def add_song
    @spotify_playlist = RSpotify::Playlist.find(
      SPOTIFY_CONFIG['default']['username'],
      SPOTIFY_CONFIG['default']['playlist_id']
    )

    song = params[:song]

    @spotify_playlist.add_tracks!([song])
  end

  private

  def authenticate
    RSpotify.authenticate(SPOTIFY_CONFIG['default']['client_id'],
                          SPOTIFY_CONFIG['default']['client_secret'])
  end
end

params[:song] is the track_uri.

Is there anything that I'm missing in order to get it working?