audio - Having trouble correctly saving a nested form that has file_field -
To upload and read file information using paperclip and ruby-mp3info, I have a form that is a playlist And makes its children, track:
& lt;% = form_for (playlist). F | & Gt%; & Lt;% if playlist.rr.a.a.a? & Gt%; & Lt; Div id = "error_explanation" & gt; & Lt; H2 & gt; & Lt;% = Plural (@playlist. Errors quote, "Error")%> Banned from saving this playlist: & lt; / H2 & gt; & Lt; Ul & gt; & Lt;% @ playlist.errors.full_messages.each do | Msg | | & Gt%; & Lt; Li & gt; & Lt;% = msg% & gt; & Lt; / li & gt; & Lt;% end% & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt;% end% & gt; & Lt; Div class = "field" & gt; & Lt;% = f.label "Name:"% " & Lt;% = f.text_field: name% & gt; & Lt; / Div & gt; & Lt; Div class = "field" & gt; & Lt;% = f.label "Image:"% " & Lt;% = f.file_field: Photo% & gt; & Lt; / Div & gt; & Lt; Br> & Lt; Div class = "field" & gt; & Lt; Br> & Lt;% = f.text_area: Description, Size = & gt; "80x3"%> & Lt; / Div & gt; & Lt; Br> & Lt; Div class = "field" & gt; & Lt;% = f.fields_for: track, track.New | FF | & Gt%; & Lt;% = ff.file_field: Audio% & gt; & Lt;% end% & gt; & Lt; / Div & gt; & Lt;% if !@playlist.tracks.blank? & Gt%; & Lt; Table id = "tracks" class = "table" & gt; & Lt; Thead & gt; & Lt; TR & gt; & Lt; Th & gt; Track & lt; / Th & gt; & Lt; Th & gt; Album & lt; / Th & gt; & Lt; Th & gt; Artist & lt; / Th & gt; & Lt; Th & gt; Label & lt; / Th & gt; & Lt; / TR & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & lt;% = f.fields_for: Tracking | FF | & Gt%; & Lt;% = Render "track_fields" ,: f = & gt; FF% & gt; & Lt;% end% & gt; & Lt; / Tbody & gt; & Lt; / Table & gt; & Lt;% end% & gt; & Lt; Div class = "verb" & gt; & Lt;% = f.submit "Save"% & gt; & Lt; / Div & gt; & Lt;% end% & gt; The problem with uploading files is that I will not be as long as I do not click on the Save button. So I read information from within my controller within the update method of my playlist controller, I try to create a new track by the following code:
def update track = @ playlist.track New () Mp3Info.open (playlist_params ["Tracks_attributes"] ["0"] ["Audio"]. Path.to_s) | Information | Track.audio_file_name = info.tag.title track.artist = info.tag.artist track. album = info.tag.album # tracknum = info.tag.tracknum.to_s End respond__ | Format | If @playlist.update (playlist_params) format.html {playlist_to playlist, notice: 'playlist was updated successfully.' } Format.json {head: no_content} else format.html {render action: 'edit'} format.json {render json: @playlist.errors, status :: unprocessable_entity} end-of-end < P> The problem is that in my update method, two examples of code tracks will be created when I only want one how can I just create an example that I am creating from within my controller and ignoring the new track Which is being made from within the form? Is there alternative ways to do this?
You are getting two records in the tracks table, because you form (with track_edit) and others you are making in the update action If you just need to save some properties of the track, Code> Parameters [: Playlist] [: Track_Aditors]] on the track that comes in.
Change the update action below: def UpdateInInInfo.open (playlist_params ["tracks_attributes"] ["0"] ["Audio "] Path.to_s. Information | Params [: playlist] [: tracks_attributes] ["0"] [: audio_file_name] = info.tag.title params [: playlist] [: tracks_attributes] ["0"] [: artist] = info.tag.artist params [ : Playlist] [: tracks_attributes] ["0"] [: album] = info.tag.album # tracknum = info.tag.tracknum.to_s end response_to do-format | If @playlist.update (playlist_params) format.html {playlist_to playlist, notice: 'playlist was updated successfully.' } Format.json {head: no_content} else format.html {render action: 'edit'} format.json {render json: @playlist.errors, status :: unprocessable_entity} end-of-end < P> This will create an associated track record for the tracks table in a playlist .
Comments
Post a Comment