I built a rails application were users can add music to their personal playlist. These playlists are only seen by the owner.
我构建了一个rails应用程序,用户可以将音乐添加到他们的个人播放列表中。这些播放列表只有所有者才能看到。
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.
有一个“my”名称空间和播放列表控制器。在控制器中,我有一个向播放列表添加歌曲并删除的方法。
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.
我理解如何通过API拉出所有的音乐,因为它是一个GET请求。但是,我该如何在iOS应用程序和web应用程序之间保持歌曲添加和删除的同步呢?
2 个解决方案
#1
2
"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.
“同步”是一个非常复杂和复杂的主题。如果您想要真正的脱机功能和真正的“同步”,您需要查看各种选项。但是简单地使用rails后端作为iOS应用的数据源是非常容易的。只需按下端点来加载或操作数据,并对iOS应用程序UI进行相同的更改。
Here are some phases, starting with the simplest:
以下是一些阶段,从最简单的开始:
Phase 1 (reloading the whole dataset every time)
阶段1(每次重新加载整个数据集)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- POST playlist/song
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- POST playlist/song GET playlist(返回播放列表中的歌曲数组)填充来自response的tableview
- POST playlist/song
- 用户单击歌曲添加发布播放列表/歌曲获取播放列表(返回播放列表中的歌曲数组),从响应填充tableview
- User clicks a song to remove
- DELETE playlist/song (returns true)
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- 删除播放列表/歌曲(返回true)获取播放列表(返回播放列表中的歌曲数组)填充tableview
- DELETE playlist/song (returns true)
- 用户点击歌曲删除删除播放列表/歌曲(返回true)获得播放列表(返回播放列表中的歌曲数组),从响应中填充tableview。
Phase 2 (loading the dataset once, then handling each add/remove individually)
阶段2(加载数据集一次,然后分别处理每个添加/删除)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- POST playlist/song (returns the song)
- add the song item to the tableview
- 将歌曲项添加到表视图
- 发布播放列表/歌曲(返回歌曲)将歌曲项添加到表视图
- POST playlist/song (returns the song)
- 用户单击歌曲添加发布播放列表/歌曲(返回歌曲),将歌曲项添加到表视图
- User clicks a song to remove
- DELETE playlist/song (returns true)
- remove the song from the tableview
- 从表视图中删除歌曲
- 删除播放列表/歌曲(返回true)从表视图中删除歌曲
- DELETE playlist/song (returns true)
- 用户单击歌曲删除播放列表/歌曲(返回true)从tableview中删除歌曲
Phase 3 (loading the dataset once, handling each add/remove individually, with latency compensation)
阶段3(加载数据集一次,分别处理每个添加/删除,延迟补偿)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- add the song item to the tableview
- POST playlist/song (returns the song)
- remove the previously added item to the tableview if the request failed
- 如果请求失败,将先前添加的项移到tableview。
- 如果请求失败,发布播放列表/歌曲(返回歌曲)删除先前添加到tableview的项
- POST playlist/song (returns the song)
- 将歌曲项添加到tableview后播放列表/歌曲(返回歌曲),如果请求失败,则将之前添加的项删除到tableview中
- add the song item to the tableview
- 用户单击歌曲将歌曲项添加到tableview POST播放列表/歌曲(返回歌曲),如果请求失败,则将之前添加的项删除到tableview
- User clicks a song to remove
- remove the song from the tableview
- DELETE playlist/song (returns true)
- add the previously removed song item to the tableview if the request failed
- 如果请求失败,将先前删除的歌曲项添加到表视图
- 如果请求失败,删除播放列表/歌曲(返回true),将先前删除的歌曲项添加到表视图
- DELETE playlist/song (returns true)
- 从表视图删除歌曲删除播放列表/歌曲(返回true),如果请求失败,将先前删除的歌曲项添加到表视图
- remove the song from the tableview
- 如果请求失败,用户单击歌曲以从表视图删除歌曲删除播放列表/歌曲(返回true),将先前删除的歌曲项添加到表视图
#2
0
Is this a problem with routing in Rails?
这是Rails中的路由问题吗?
See the guide of Rails routing
参见Rails路由指南
http://guides.rubyonrails.org/routing.html
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.
您需要发送一个包含_method param的请求,以便从iOS应用程序中模拟rails PUT/DELETE方法。
Can iOS devices send PUT requests to Rails apps?
iOS设备可以向Rails应用程序发送PUT请求吗?
#1
2
"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.
“同步”是一个非常复杂和复杂的主题。如果您想要真正的脱机功能和真正的“同步”,您需要查看各种选项。但是简单地使用rails后端作为iOS应用的数据源是非常容易的。只需按下端点来加载或操作数据,并对iOS应用程序UI进行相同的更改。
Here are some phases, starting with the simplest:
以下是一些阶段,从最简单的开始:
Phase 1 (reloading the whole dataset every time)
阶段1(每次重新加载整个数据集)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- POST playlist/song
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- POST playlist/song GET playlist(返回播放列表中的歌曲数组)填充来自response的tableview
- POST playlist/song
- 用户单击歌曲添加发布播放列表/歌曲获取播放列表(返回播放列表中的歌曲数组),从响应填充tableview
- User clicks a song to remove
- DELETE playlist/song (returns true)
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- 删除播放列表/歌曲(返回true)获取播放列表(返回播放列表中的歌曲数组)填充tableview
- DELETE playlist/song (returns true)
- 用户点击歌曲删除删除播放列表/歌曲(返回true)获得播放列表(返回播放列表中的歌曲数组),从响应中填充tableview。
Phase 2 (loading the dataset once, then handling each add/remove individually)
阶段2(加载数据集一次,然后分别处理每个添加/删除)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- POST playlist/song (returns the song)
- add the song item to the tableview
- 将歌曲项添加到表视图
- 发布播放列表/歌曲(返回歌曲)将歌曲项添加到表视图
- POST playlist/song (returns the song)
- 用户单击歌曲添加发布播放列表/歌曲(返回歌曲),将歌曲项添加到表视图
- User clicks a song to remove
- DELETE playlist/song (returns true)
- remove the song from the tableview
- 从表视图中删除歌曲
- 删除播放列表/歌曲(返回true)从表视图中删除歌曲
- DELETE playlist/song (returns true)
- 用户单击歌曲删除播放列表/歌曲(返回true)从tableview中删除歌曲
Phase 3 (loading the dataset once, handling each add/remove individually, with latency compensation)
阶段3(加载数据集一次,分别处理每个添加/删除,延迟补偿)
- UIViewController is loaded
- GET playlist (returns an array of songs in a playlist)
- populate tableview from response
- 填充tableview从响应
- 获取播放列表(返回播放列表中的歌曲数组)填充tableview
- GET playlist (returns an array of songs in a playlist)
- UIViewController从响应中加载GET播放列表(返回播放列表中的歌曲数组)填充tableview
- User clicks a song to add
- add the song item to the tableview
- POST playlist/song (returns the song)
- remove the previously added item to the tableview if the request failed
- 如果请求失败,将先前添加的项移到tableview。
- 如果请求失败,发布播放列表/歌曲(返回歌曲)删除先前添加到tableview的项
- POST playlist/song (returns the song)
- 将歌曲项添加到tableview后播放列表/歌曲(返回歌曲),如果请求失败,则将之前添加的项删除到tableview中
- add the song item to the tableview
- 用户单击歌曲将歌曲项添加到tableview POST播放列表/歌曲(返回歌曲),如果请求失败,则将之前添加的项删除到tableview
- User clicks a song to remove
- remove the song from the tableview
- DELETE playlist/song (returns true)
- add the previously removed song item to the tableview if the request failed
- 如果请求失败,将先前删除的歌曲项添加到表视图
- 如果请求失败,删除播放列表/歌曲(返回true),将先前删除的歌曲项添加到表视图
- DELETE playlist/song (returns true)
- 从表视图删除歌曲删除播放列表/歌曲(返回true),如果请求失败,将先前删除的歌曲项添加到表视图
- remove the song from the tableview
- 如果请求失败,用户单击歌曲以从表视图删除歌曲删除播放列表/歌曲(返回true),将先前删除的歌曲项添加到表视图
#2
0
Is this a problem with routing in Rails?
这是Rails中的路由问题吗?
See the guide of Rails routing
参见Rails路由指南
http://guides.rubyonrails.org/routing.html
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.
您需要发送一个包含_method param的请求,以便从iOS应用程序中模拟rails PUT/DELETE方法。
Can iOS devices send PUT requests to Rails apps?
iOS设备可以向Rails应用程序发送PUT请求吗?