2

I'm trying the Gentle Introduction to CarrierWave-tutorial by using the web-framework Sinatra. When I run my app it starts just fine and the app asks me to upload a file and it does so without any problems. However, while uploading the file, the app throws me an "undefined method `join' for # String:0x3480d50 "-error.

I've looked around a little bit on the internet and I found this issue at github where they say that the error may be due to incompatibilities between Rack and Sinatra or for having installed duplicate versions of Sinatra.

Does anybody know what's happening?

My uploader_app:

require 'carrierwave'
require 'sinatra'
require 'sqlite3'
require 'sequel'
require 'carrierwave/sequel'

DB = Sequel.sqlite
DB.create_table :uploads do 
    String :file    
end

# uploader
class MyUploader < CarrierWave::Uploader::Base
    storage :file
end

# model
class Upload < Sequel::Model
    mount_uploader :file, MyUploader
end

# sinatra app
get '/' do
     @uploads = Upload.all
     erb :index
end

post '/' do
    upload = Upload.new
    upload.file = params[:image]
    upload.save
    redirect to('/')
end

__END__

@@ index
<!DOCTYPE html>
<html>
    <body>
        <div>
            <form action="/" method="post" enctype="multipart/form-data">
                <input type="file" name="image" />
                <input type="submit" name="submit" value="Upload" />
            </form>
                <% @uploads.each do |upload| %>
                    <img src="<%= upload.file.url %>" />
                <% end %>
        </div>
    </body>
</html>
ljnissen
  • 139
  • 1
  • 12
  • Would you share the stack trace with us? – ian Jan 26 '15 at 04:52
  • @iain Sure [link](https://gist.github.com/ljnissen/6f65363175ceb16cd97d) It actually gives me two errors: One NoMethodError as above and one "TypeError: Can't convert nil into string". Any help is much appreciated. – ljnissen Jan 26 '15 at 18:40
  • You're not going mad. I just installed it and ran it, got the same error. I'm running Ruby 2.1.2 and I sandboxed the gems using bundler, but still failed. I'll see what I can find. – ian Jan 28 '15 at 12:38

1 Answers1

2

The error is occurring on this line in the Carrierwave Library:

path = encode_path(file.path.gsub(File.expand_path(root), ''))

It fails because root is nil, so File.expand_path(root) raises an error. I don't know why root isn't set, but the following code (that I modified from this answer) worked for me:

CarrierWave.configure do |config|
  config.root = settings.root
end

I just added it to the code after declaring the Sequel class and before defining the route. Probably best to stick it in a configure block too. Note that settings.root in the code above is Sinatra's root setting.

This doesn't seem to be caused by the current problems between Rack 1.6.0 and Sinatra 1.4.5 as that's what I was running, although I'm on Ruby v2.1.2 as I mentioned in the comments above.

Depending on what you want, Sinatra's root might not be the best place to put things, as I ended up with a directory inside the project root called "uploads" which had the files in, but config.root obviously needs to be set to something.

Hope that helps.

Community
  • 1
  • 1
ian
  • 12,003
  • 9
  • 51
  • 107
  • I think there is a connection with the Rack 1.6.0 – Sinatra 1.4.5 issue. That problem is in the exception handler, so if you fix the original error (as you do here) the `undefined method join` error won’t appear and obscure the original. – matt Jan 29 '15 at 12:47
  • @matt you're right, I forgot the question originally was about the join error and not the type error (even though it really *should* be that way round:) – ian Jan 29 '15 at 16:47