carrierwaveuploader / carrierwave-mongoid

Mongoid support for CarrierWave
MIT License
355 stars 165 forks source link

file is getting saved in db but not uploading to a directory specified. #186

Closed VishalTaj closed 6 years ago

VishalTaj commented 6 years ago

I'm building a Rails application in which MongoDB as database and i have created a form to upload files to the rails public directory and save details in the database.

For uploading, I use gem called carrierwave. when i tried to save from my form i have managed to save the uploaded file details in the database but I'm not able to upload the file into the public directory. i will write down code which i have tried if any changes needed please assist me on it.

i have configured uploader as follows:

 class DocumentUploader < CarrierWave::Uploader::Base
   storage :grid_fs

   def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

   def cache_dir
     "#{Rails.root}/tmp/uploads"
   end
end

in model:

class Branch
  include Mongoid::Document
  ---
  mount_uploader :icon, DocumentUploader
end

in controller:

class BranchesController < ApplicationController
 def new
   @branch = Branch.new
 end

 def create
   @branch = Branch.new(branch_param)
   @branch.save  
 end

 private 

   def branch_param
     params.require(:branch).permit(:name, :icon)
   end
end

and the form view is as follows:

<%= form_for @branch do |f| %>
  ----
  <%= f.file_field :icon %>
  <%= f.submit 'save' %>
<% end %>

in my Gem list i have added few gems like

gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs'

Thanks in advance