Reputation: 251
So I have a pretty straight forward implementation of Carrierwave for image upload. I've mounted the uploader on an ActiveRecord Model:
class MenuItem < ActiveRecord::Base
attr_accessible :description, :menu_id, :name, :active, :photo
mount_uploader :photo, PhotoUploader
belongs_to :menu
before_save :default_values
validates :name, presence: true
validates :menu_id, presence: true
validates :description, length: { maximum: 250 }
private
def default_values
if (self.active.nil?)
self.active = true
end
end
end
The form I'm using combines editing / creating the above MenuItem along with attaching a file:
<%= form_for(@menuitem) do |f| %>
<%= render 'shared/error_messages', object: @menuitem %>
<%= f.file_field :photo, :id => "photo" %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.label :active %>
<%= f.check_box :active, :value => @menuitem.active %>
<%= f.submit "Save Menu Item", class: "btn btn-large btn-primary" %>
<%= f.submit "Cancel", class: "btn btn-large btn-secondary" %>
<% end %>
Everything works find in the case of Edit / Update.
However, on New / Create I'm getting a validation error on the :photo attribute which simply says "Photo is Invalid". The Uploader definition is vanilla:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
after :store, :make_thumbnail
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def make_thumbnail(file)
a = filename.split('/')
key = store_dir + "/" + a[0] + "/" + model.id.to_s + "_thumb.jpg"
job = Blitline::Job.new(url)
job.application_id = ENV['BLITLINE_APPLICATION_ID']
sf = job.add_function("resize_to_fill", {:height => 100, :width => 100})
sf.add_save("thumbnail", key, ENV['S3_BUCKET'])
bs = Blitline.new
bs.jobs << job
res = bs.post_jobs
end
end
And the Controller methods are simple also
class MenuItemsController < ApplicationController
def new
@menuitem = MenuItem.new
@menuitem.menu_id = params[:menuid]
@menuitem.active = true
end
def create
@menuitem = MenuItem.new(params[:menu_item])
if @menuitem.save
flash[:success] = "Menu Item created!"
redirect_to menu_path(@menuitem.menu)
else
render 'new'
end
end
I thought maybe the issue was related to the reference to model.id
in the store_dir
method of the uploader and that the ID wasn't available since the record hadn't been created yet - but removing that didn't help the issue.
Again, the Edit / Update path that uses the same form and simply calls menuitem.update_attributes(params[:menu_item])
works fine.
I'm sure there's something obvious I'm missing. Any suggestions would be greatly appreciated!
Upvotes: 4
Views: 3866
Reputation: 285
Coming to this very late but I just had the same issue.
What was wrong for me was MiniMagick
I had to strip back my code and call photo.save!
This gave me the error Avatar Failed to manipulate with MiniMagick
I removed Minimagick from my uploader file and it worked. So I need to fix my MiniMagick installation not my code. Hope this helps someone.
Upvotes: 0
Reputation: 23
You need to set following in Carrierwave.rb file
config.validate_unique_filename = false
config.validate_filename_format = false
config.validate_remote_net_url_format = false # this is important
This works for me. Personally, I use carrierwave-aws now as it's much leaner than Fog.
Upvotes: 0
Reputation: 31
You can fix the error by setting up - ur_net_format validation in carrierwave.rb initializer file to false.
Like so:
ruby
config.validate_unique_filename = true
config.validate_filename_format = true
config.validate_remote_net_url_format = true
Hope this helps anyone.
Upvotes: 1
Reputation: 4315
I've got a suggestion :
Whitelisting formats (in your uploader):
def extension_white_list
%w(jpg jpeg gif png)
end
Upvotes: 0