A
A
Andrey2016-11-22 12:01:08
ruby
Andrey, 2016-11-22 12:01:08

(Sinatra + CarrierWave) Why is the image not loading?

For some reason, the files that I upload using Carrierwave are not saved in uploads?
In the Posts table, I created the image field, which is filled with the uploaded file.
And /public/uploads does not fit files.
Tell me, what could be the reason?
There are no image logs either.

SQL (0.9ms)  INSERT INTO "posts" ("title", "summary", "body", "vendor", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  

When debugging
[1] pry(#<Sinatra::Application>)> params
=> {"post"=>{"title"=>"sssssssssss", "summary"=>"", "body"=>"ss", "vendor"=>""},
 "image"=>
  {:filename=>"new1.jpg",
   :type=>"image/jpeg",
   :name=>"image",
   :tempfile=>#<File:/tmp/RackMultipart20161122-3054-lu5e6w.jpg>,
   :head=>
    "Content-Disposition: form-data; name=\"image\"; filename=\"new1.jpg\"\r\nContent-Type: image/jpeg\r\n"}}

model
class Post < ActiveRecord::Base
  mount_uploader :image, ImagesUploader
end

migration
class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :image
    end
  end
end

controllers
# form for new posts
get '/new' do
  @post = Post.new
  erb :"new"  
end

# save
post '/posts' do
  @post = Post.new(params[:post])

  if @post.save
    redirect :"/"
  else
    redirect :"/new"
  end
end

ImagesUploader
class ImagesUploader < CarrierWave::Uploader::Base
  # include CarrierWave::MiniMagick

  storage :file

  def store_dir
    "uploads/images/#{model.id}"
  end

  # version :thumb do
  #   process resize_to_fit: [400, 400]
  # end

  # def extension_whitelist
  #   %w(jpg jpeg png gif)
  # end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Grudinkin, 2016-11-22
@andreychumak

I don't know sinatra, but what do you see

class Post < ActiveRecord::Base
  mount_uploader :image, 
end

what I see in the documentation for CarrierWave:
class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

after "@post = Post.new(params[:post])" let's try to add "@post.image = params[:image]"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question