Reputation: 2074
I built a rails application were users can add music to their personal playlist. These playlists are only seen by the owner.
There is a "my" namespace with a playlists controller. Inside the controller I have a method to add a song to the playlist and remove.
def add
@post = Post.find params[:post_id]
@playlist = current_user.playlist
@playlist.posts << @post
redirect_to root_path, :flash => { :alert => "Added to playlist!"}
end
def remove
@playlist = current_user.playlist
@playlist.posts.delete(params[:post_id])
redirect_to my_playlist_path, :flash => { :alert => "Removed from playlist!"}
end
I understand how to pull all the music through the API since it is a GET request. But how would I keep the adding and removing of songs from the playlist in sync between the iOS app and web app.
Upvotes: 3
Views: 731
Reputation: 61
"in sync" is a really complicated and involved subject. If you want true offline capabilities, and true "sync", you'll need to look into various options out there. But to simply use your rails backend as the data source for your iOS app is pretty easy. Just hit the endpoints to load or manipulate the data, and make the same changes to the iOS app UI.
Here are some phases, starting with the simplest:
Phase 1 (reloading the whole dataset every time)
Phase 2 (loading the dataset once, then handling each add/remove individually)
Phase 3 (loading the dataset once, handling each add/remove individually, with latency compensation)
Upvotes: 2
Reputation: 358
Is this a problem with routing in Rails?
See the guide of Rails routing
http://guides.rubyonrails.org/routing.html
resources :playlists do
get :add, on: :member
delete :remove, on: :member
end
Or you can use create
and destroy
actions defined in resources
instead of add
and remove
.
And you need to send a request including _method
param to emulate rails PUT/DELETE methods from your iOS app.
Can iOS devices send PUT requests to Rails apps?
Upvotes: 0