I have an interesting problem with the step of creating an object in my Rails 3 music application.
I have two models (not for real models, but for simplicity): Playlist Song
Playlist has_many Songs; The song belongs to the playlist.
Each playlist object must have an exact number of songs. Each song must belong to a playlist.
With this in mind, I want to create a playlist creation process that also includes creating all the necessary songs at once.
Another thing is that to obtain Song data, the user enters a query (which I will not save in the Song model), which then collects data from the API. This is the data that should be used to create the Song object. So I canβt (don't think?) Use the traditional form_for.
Instead, I use remote form_tag. This form requests a request, and then uses an Ajax request to retrieve the data, which is placed in a temporary Song object and then displayed on a line on the playlist creation page using the Song view. This form is reused for all required Song objects for the playlist.
So, the idea is that when the user has entered the required number of requests (that is, added the required number of songs to the playlist), they will be presented with a new button that allows them to send information about the playlist and continue the process. Then a playlist will be created with all the song objects that were created using Ajax as children.
In fact, I cannot find a way for this to work in an elegant way. Although I create Song objects via Ajax, they are not saved anywhere, and they donβt know which playlist they should be added (since the playlist object does not exist in the database yet.) Therefore, when I go to the next step, I am left without all Song data. I studied using nested forms with accepts_nested_attributes_for, but I can't figure out how to use it with my setup (non-model form using Ajax.)
So I'm stuck. If anyone can help, he will be very grateful.