ruby - Building has many through relationship rails from existing records -
I am a bit stuck on a form for many through the relationship. At the moment, my models are that many of the songs The Settleist and vice versa can be through allocation.
I am currently working on an edit page in which a user can add songs to a setlist. The scene currently looks like this:
& lt; H1 & gt; Edit a Setlist & lt; / H1> & Lt; Div class = "row" & gt; & Lt; Div class = "span8" & gt; Do & lt;% = form_for (@setlist) | F |% & gt; & Lt;% = f.label: Date, "Date Set"% & gt; & Lt; Span & gt; & Lt;% = f.date_select: Date% & gt; & Lt; Span & gt; & Lt; Div & gt; & Lt; Div id = "library" & gt; & Lt;% = Renderer 'Library'% & gt; & Lt; / Div & gt; & Lt;% = Renders 'songs_in_set'% & gt; & Lt; / Div & gt; & Lt;% = f.submit "Save", Category: "BTN BTN-Big BTN-Primary"%> & Lt;% end% & gt; & Lt; / Div & gt; & Lt; / Div & gt;
The above mentioned library partial:
& lt; Table class = "table table-striped table-limit" & gt; & Lt; Thead & gt; & Lt; Th & gt; Title & lt; / Th & gt; & Lt; Th & gt; Artist & lt; / Th & gt; & Lt; Th & gt; Add to set & lt; / Th & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & Lt;% @ songs.each do | Song & Gt%; & Lt; TR & gt; & Lt; Td> & Lt;% = song.title% & gt; & Lt; / Td> & Lt; Td> & Lt;% = song.artist% & gt; & Lt; / Td> & Lt; Td> & Lt;% = link_to "ADD", '#'% & gt; & Lt; / Td> & Lt; / TR & gt; & Lt;% end% & gt; & Lt; / Tbody & gt; & Lt; / Table & gt;
I want to change that link in partial library in a link to create a new allocation, thus adding a song to the set list.
Bits related to your controllers: Setlist Administrators:
def Edit Songs = songs. All (Order: 'Title') @setlist = Setlist.find (Params [ : Id]) @allocations = @setlist.allocations End DEF update @setlist = Setlist.find (params [: id]) if @setlist.update_attributes (params [: setlist]) flash [: success] = "saved!" Redirect_to Setlist_path (@setlist) provide another 'Edit' end-end
and allocation controller:
def new @allocation = allocation.new end
@allocation = allocation.New (consultation [allocation]) if @ allocation. [: Success] = "added songs" redirect_to edit_setlist_path (@ allocation .setlist_id) and flash [: unsuccessful] = "edit_setlist_path" (error allocation .setlist_id)
termination
< P> I know that I will do something with this setlist.allocations.build lines, but I'm having trouble getting the right parameters (each individual song id and the sitelist ID). I have tried putting a form for the assistant within the song. What to do loop, but it did not seem to work. I'm losing a bit, so any signal in the right direction will be appreciated. Thanks in advance
Try adding it to Setlist
:
accepts: _Advanced_right: for: allocation, dependent => : Delete
, then you can create an area of nesting area within your Setlist form. Partial in the library:
& lt; Td> For & lt;% = f.fields_: Allocation | FF | & Gt%; & Lt;% = f.hidden_field: song_id, song.id% & gt; ... & lt;% end% & gt; & Lt; / TD & gt;
If you want to make allotment for already existing songs, you should work, but whenever you are making a song in the same form, Something else may be required. I had a problem that was not long ago, and I was not able to get a clean solution, but tell me and I can also share it with ...
Comments
Post a Comment