0
votes

I'm trying to upload a file using Rails, Mongoid and Carrierwave. When I click submit, the value of "image" in the POST request is {url : null}. When I use logger to view the value of "image" from the create action in my controller, the value is "C:\\fakepath\\tree.png" so I can't just save it directly using File.open to get the file since this is not the real path. This is the first time I'm writing a form with an uploader so I'm not really sure what I'm doing.

Here's my form for uploading the image:

<h1>New Post</h1>
<form id="new-post" name="post" enctype="multipart/form-data">
  <div class="field">
    <label for="name"> Name</label><br />
    <input type="text" name="name" id="name" value="<%= name %>" >
  </div>
  <div class="field">
    <label for="image"> Image</label><br />
    <input type="file" name="image" id="image" value="<%= image %>" >
  </div>
  <div class="field">
    <label for="content"> Content</label><br />
    <textarea rows="4" cols="50" name="content" id="content" value="<%= content %>" ></textarea>
  </div>
  <div class="actions">
    <input type="submit" value="Create Post" />
  </div>
</form>

Here's what I added to support carrierwave:

Gemfile:

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

PostModel:

attr_accessible :name, :content, :image
mount_uploader :image, ImageUploader

ImageUploader:

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

1 Answers

0
votes

Try this guide to get you started: http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm

The fakepath thing is fine, you really don't care about the path itself, you are concerned about the byte data that was uploaded.