Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:40:38+00:00 2026-05-26T12:40:38+00:00

I’m using CarrierWave to store files in gridfs, but having problems with opening them

  • 0

I’m using CarrierWave to store files in gridfs, but having problems with opening them from my model.
Here are my configs:

/config/initialize/carrierwave.rb

 CarrierWave.configure do |config|
  config.grid_fs_database = Mongoid.database.name
  config.grid_fs_host = Mongoid.config.master.connection.host
  config.storage = :grid_fs
  config.grid_fs_access_url = "/files"
end

/app/controllers/gridfs_controller.rb

 /require 'mongo' 
        class GridfsController < ActionController::Metal
          def serve
            gridfs_path = env["PATH_INFO"].gsub("/files/", "")
            begin
              gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
              self.response_body = gridfs_file.read
              self.content_type = gridfs_file.content_type
            rescue
              self.status = :file_not_found
              self.content_type = 'text/plain'
              self.response_body = ''
            end
          end
        end

/app/uploaders/list_uploader.rb

class ListUploader < CarrierWave::Uploader::Base
  storage :grid_fs
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

and in routes

match "/files/uploads/*path" => "gridfs#serve"

So, I have a model, which have a text file

class Campaign
  include Mongoid::Document
  mount_uploader :list, ListUploader

When I’m calling something like <%=link_to "List", @campaign.list.url %> from my view, it opens fine. But when I’m trying something like File.open("#{campaign.list.url}", "r") from campaign model, it fails. It gives me false even when I’m calling File.exists?("/files/uploads/campaign/list/4eb02c4d6b1c0f02b200000b/list.txt"), which is a proper url for that file. So, the question is how should I call it, to open the file from model? And for some reasons, it is important to open it from model. Any suggestions would help, thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:40:38+00:00Added an answer on May 26, 2026 at 12:40 pm

    Carrierwave url with mongodb gridfs is not a physical path. Its merely a logical route to download the file from gridfs. Thats why you cannot access it from ruby File.open. Check out the below snippet from rails console trying to open the file from gridfs

    File.open(User.first.image.pic.url,'r')
    Errno::ENOENT: No such file or directory - /images/uploads/e5a1007d34.jpg
    

    see it throw No such file or directory., So you have to download a file instead opening by

    >> require 'open-uri'
    >> open('image.jpg', 'wb') do |file|
    ?> file << open('http://0.0.0.0:3000' + (User.first.image.pic.url)).read
    >> p file
    >> end
    #<File:image.jpg>
    => #<File:image.png (closed)>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.