1

I'm trying to build an app in Rails which will take audio file uploads and read metadata off them to populate a database. I'm using the Taglib-ruby gem to handle various file types. The uploads seem to be working on their own, but Taglib considers any file given to it as null.

Here's my controller:

class UploadsController < ApplicationController
    require 'taglib'

    def new
    end

    def create
        file = params[:upload]
        TagLib::FileRef.open(file) do |fileref|
            unless fileref.null?
                tag = fileref.tag
                # properties = fileref.audio_properties
                @song = Song.new(title: tag.title, artist: tag.artist, album: tag.album,
                    year: tag.year, track: tag.track, genre: tag.genre)
                if @song.save
                    redirect_to songs_path
                else
                    render 'new'
                end
            else
                raise "file was null"
            end
        end
    end
end

and my view for form submission:

<h1> Upload </h1>

<%= form_tag(url: { action: :create }, html: { multipart: true }) do %>

    <%= label_tag :upload, "Scan your song:" %>
    <%= file_field_tag :upload, multiple: true %>

    <br />
    <%= submit_tag "Submit" %>

<% end %>

Taglib itself seems to be working - adding "require 'taglib'" removed the error I had been getting in regards to that, and a mock-up I made of this outside of rails worked fine (so the files I'm using are also not the problem). Every time I run this, the control flow hits my raise command, and no record is saved. It's clear that fileref.null? is returning true, which suggests to me that there's something wrong with the upload process... but I'm not sure what.

Ideally, I'd like to use the multiple uploads option and run this process on each file sequentially, but I can't even get a single upload to register as anything but null.

anothermh
  • 9,815
  • 3
  • 33
  • 52
Anrothan
  • 500
  • 5
  • 13
  • Is the `file` argument you pass to `open` an absolute file system path? E.g. what happens if you do `File.exists?(file)` at that place? – robinst Aug 03 '14 at 08:47
  • @robinst, I tried it and it returns false. That said, I also confirmed that the params hash contains as one of its entries (for example) `{"upload"=>"1. Rachin_ in Rhythm.mp3"}`. It seems like it's not getting the file appropriately through the params hash, but I can't tell why not. – Anrothan Aug 07 '14 at 06:38
  • So this is really a question about "how to get path to uploaded file in rails", it has nothing to do with taglib-ruby. I would change the question so that others can help. – robinst Aug 08 '14 at 10:19
  • Yeah, the more I look at it, it's looking that way. I'm playing with some ideas about why it may not be working now. – Anrothan Aug 09 '14 at 01:39

0 Answers0