I have the following code (working) to create playlists:
Controller:
def create
@playlist = current_user.playlists.build(playlist_params)
if @playlist.save
redirect_to root_url
else
redirect_to root_url
end
end
def destroy
end
private
def playlist_params
params.require(:playlist).permit(:content, :name)
end
View:
<%= form_for(@user.playlists.create, remote:true) do |f| %>
<%= f.text_field :content, id: "playlistContent", :validate => true %>
<%= f.text_field :name, id: "playlistName", placeholder: "Enter Playlist Name", :validate => true %>
<%= f.submit "Save Playlist", id: "savePlaylist", remote:true %>
<% end %>
Now I wish to allow the user to add a new song to playlist content
. I'm using a typeahead where a user enters the Playlist name, and upon selecting the name it will fill in a hidden ID field with the playlist ID (in case of multiple playlists with the same name). If necessary, I will include the full typeahead code here. I am attempting to update the playlist along the following lines (based on this earlier question: Problems keeping an object in an array, Ruby issues and Rails issues )
My main issue is trying to pass through the ID of the playlist the user selects to the rails form, (rails being server side and not having ability of client-side methods like jQuery to get the text field value as I understand). The update_attribute
and update_column
methods seemd to be limited to passing through (name, value)
, and I can't figure out how to pass through the ID so I can push through the new song to the existing playlist content
array.