Carrierwave remove picture and set back to default - ruby-on-rails-3

I'm using Rails 3 with carrierwave gem.
I managed to removed the avatar image from my user model like this:
#user.remove_photo!
and it works perfectly. However, I want to set the picture url for that user back to the default_url (which is the image that every user has until they upload one).
Any ideas how?
--
Image display code:
<%= image_tag(#user.photo.send(:layout).url, :alt => #user.full_name, :class => 'photo large') %>
default_url code:
def default_url
"default_photo.jpg"
end

From the docs:
https://github.com/carrierwaveuploader/carrierwave
In many cases, especially when working with images, it might be a good
idea to provide a default url, a fallback in case no file has been
uploaded. You can do this easily by overriding the default_url method
in your uploader:
class MyUploader < CarrierWave::Uploader::Base
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
end
Or if you are using the Rails asset pipeline:
class MyUploader < CarrierWave::Uploader::Base
def default_url
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
end
end

You need to force update the field in the database to null, after that carrierwave will use the default_url, you can do this by calling Model.where(<CONDITIONS>).update_all(photo: nil) or instance.update_column(photo: nil)

Related

carrierwave minimagick converted svg not compatible

I have a carrierwave installed with mini-magick on a rails-3-2 project.
I am facing a problems creating versions for uploaded svg images.
My uploader code is as follows
class SVGUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
process resize_to_fit: [400, 400]
version :thumb do
resize_to_fit(140, 140)
end
def extension_white_list
[:svg]
end
def store_dir
#dir ||= if ENV['PARALLEL_TEST_GROUPS']
"system/uploads/#{ENV['TEST_ENV_NUMBER']}/#{Rails.env}/#{model.class.to_s.underscore}/#{model.name}"
else
"system/uploads/#{Rails.env}/#{model.class.to_s.underscore}/#{model.id.to_s}"
end
end
end
The problem is when ever I upload any svg image, it takes a very long time to convert. and when I try to display the image browsers don't render them.
Anybody faced this issue? Please help.
I saving without format svg to png
I solve this away:
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
include CarrierWave::MiniMagick
process :set_content_type
version :super_thumb, :if => :is_picture? do
process :resize_to_fill => [50, 50]
end
protected
def is_picture?(picture)
return false if set_content_type(picture).include?('svg')
set_content_type(picture).include?('image')
end
end

Browse button is not working for carrierwave and uplodify

Hi I am uploading the video from my rails app.
For this I am using carrierwave gem.
I am referring this tutorial for it.
But when I tried to upload the video from my computer by using Browse button it is not allowing me to browse.
When I checked in the firebug I have getting this type of error
"NetworkError: 404 Not Found - http://localhost:3000/videos/uploadify.swf?preventswfcaching=1393936089114"
I tried to solve it but I didn't get any solution for it.
Here is my
videos controller
class VideosController < ApplicationController
def index
end
def show
#video = Video.find(params[:id])
end
def new
#video = Video.new(video_params)
end
def create
#video = Video.new(video_params)
if #video.save
render :nothing => true
else
render :nothing => true, :status => 400
end
end
private
def video_params
params.require(:video).permit(:attachment, :zencoder_output_id, :processed, :video, :meta_info) if params[:gallery]
end
end
model - video.rb
class Video < ActiveRecord::Base
belongs_to :gallery
mount_uploader :video, VideoUploader
end
uploader file
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
storage :file
def store_dir
"videos/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process encode_video: [:mp4, resolution: "200x200"]
def extension_white_list
%w(avi mov mkv mpg mpeg mp4 m4v flv)
end
end
I am using rails 4 and ruby 2.1.0

File download link in ActiveAdmin

How do I create a file download link in ActiveAdmin to download an uploaded file?
I tried something like this:
action_item :only=> :show do
link_to('Download file', [#user, :upload_file])
end
The action_item will establish the button in the taskbar, but it does not create the actual route or the controller's action method.
You can achieve the download functionality by creating a custom member_action in user.rb
# In app/admin/users.rb
action_item only: [:show] do
link_to('Download File', download_admin_user_path(resource)) if resource.upload_file.present?
end
member_action :download, method: :get do
user = User.find(params[:id])
send_file user.upload_file
end
or, my preference, just leverage the RESTful show action in upload_file.rb
# In app/admin/users.rb
action_item only: [:show] do
# NOTE: remove period from file extension
file_ext = resource.upload_file.file_extension.gsub('.', '')
link_to('Download File', download_admin_upload_file_path(resource.upload_file, format: file_ext)) if resource.upload_file.present?
end
# In app/admin/upload_files.rb
controller do
def show
if params[:format] == 'txt' # example of a known mimetype, e.g. text file
send_data resource.path, type: 'text/plain'
elsif params[:format] == resource.file_extension # example of unknown mimetype
send_file resource.path
# let ActiveAdmin perform default behavior
else
super
end
end
end
cite: http://activeadmin.info/docs/8-custom-actions.html
I don't know the complete code that you are using but that should work -
routes.rb -
resources :users do
collection do
get :upload_file
end
end
controller -
def upload_file
send_file #user.upload_file.path, :type => 'application/pdf', :filename => #user.permalink
end
View -
<%= link_to 'Download file', upload_file_users_path(#user) %>

Fog::Storage::Rackspace::NotFound error when using Carrierwave to upload to Rackspace

Everyone: I already searched the error before I posted this to Stackoverflow, so no need to point me to this: groups.google.com/forum/?fromgroups=#!topic/carrierwave/ It's not the same problem.
I'm using Carrierwave so users can upload files to my Rackspace container. But when I Submit from my site (on my local machine, still in test mode), I get a Fog::Storage::Rackspace::NotFound app/controllers/authors_controller.rb:8:in `update' error. My Rackspace container is called kontainer.ofstuff. Here's my code:
pic_uploader.rb:
class PicUploader < CarrierWave::Uploader::Base
include Rails.application.routes.url_helpers
storage :fog
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
model author.rb
class Author < ActiveRecord::Base
attr_accessible :stuff, :profilepic
mount_uploader :pic, PicUploader
def dostuff
end
end
carrierwave.rb is in the config/initializers directory
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'Rackspace',
:rackspace_username => 'myusername',
:rackspace_api_key => '98765asecretnumber3'
})
config.fog_directory = 'kontainer.ofstuff'
config.fog_host = 'https://34567secretnumberiiiii.ssl.cf2.rackcdn.com'
end
controller authors_controller.rb
class AuthorsController < ApplicationController
def update
#author = Author.find(params[:id])
#booklist = Book.where(:author_id => #author.id)
#line 7
if #author.update_attributes(params[:author])
sign_in #author
redirect_to #author
else
render 'profileinfo'
end
end
end
edit.html.erb:
<%= f.file_field :pic %>
<%= f.submit "Save Author Info" %>
When I had this code 'uploading'/storing to a file, this worked fine. Perhaps f.submit does not work with Carrierwave? If not...where do I find the correct code for submitting?
Any ideas what the trouble is?
I also got this error.
Solved by adding this to uploaded code:
class MyUploader < CarrierWave::Uploader::Base
....
def remove!
begin
super
rescue Fog::Storage::Rackspace::NotFound
end
end
end
I kind of had the same problem, but for me it turned out that I needed to make the container multiple times with the same name but for all the regions. I have no idea why it worked after that, but I guess that's something to try?
Update 11-7-2012
So Carrierwave had some updates since my answer. I was able to get a more stable upload through some trial and error. Here's what I did:
Updated carrierwave gem to 0.7.0
Logged into Rackspace and deleted containers for all the regions.
Added one single container. It doesn't matter which region, whichever is more applicable to you.
Made the container public (Enable CDN)
Copied the Public HTTP CDN link for the container
Updated my /config/initalizers/carrierwave.rb file:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'Rackspace',
:rackspace_username => '[Your Rackspace Username]',
:rackspace_api_key => '[Your Rackspace API key]'
}
config.fog_directory = '[The name of the container you created]'
if Rails.env.production? || Rails.env.staging?
config.asset_host = '[The Public HTTP CDN url for the container]'
end
end
As a note: I configured my uploader to use storage:fog when the environment is production or staging. Otherwise I use the default local file system.
The main thing to note is that carrierwave changed the config 'fog_host' to 'asset_host.'
For what it's worth: I was having this same problem after migrating from AWS to Rackspace. The error was being thrown because part of updating the file is deleting the old file. In my case, the old file was on S3, not on Rackspace, so carrierwave got upset.
This seemed to be a case of Wait A Few Months & install updated gem. The problem pretty much went away. I also got rid of Rackspace & went to Amazon S3, although I had tried S3 earlier with the same issues. I'm crediting the solution to the updated Carrierwave gem.

Error in Image crop using MiniMagick with carreirwave in Rails 3

I am trying to crop image using mini magick gem with carrierwave. I am facing following issue when i am creating user.
Errno::ENOENT in UsersController#create
No such file or directory - identify -ping /tmp/mini_magick20120919-5600-ai31ph.jpg
My Code :
model/user.rb
class User < ActiveRecord::Base
attr_accessor :crop_x, :crop_y, :crop_h, :crop_w
attr_accessible :username,:profile
after_update :reprocess_profile, :if => :cropping?
#field :username
mount_uploader :profile, ProfileUploader
def cropping?
!crop_x.blank? and !crop_y.blank? and !crop_h.blank? and !crop_w.blank?
end
def profile_geometry
#img = MiniMagick::Image.open(self.profile.large.path)
##geometry = {:width => img[:width], :height => img[:height] }
end
private
def reprocess_profile
#puts self.profile.large.path
#img = MiniMagick::Image.open(self.profile.large.path)
#crop_params = "#{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"
#img.crop(crop_params)
#img.write(self.profile.path)
#profile.recreate_versions!
end
end
uploader/profile_uploader.rb
# encoding: utf-8
class ProfileUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
#include CarrierWave::RMagick
# include CarrierWave::ImageScience
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
version :large do
end
version :thumb do
process :resize_to_fill => [100, 100]
end
end
What would be the problem ? please suggest any solution.
Need to install imagemagick.
In ubuntu:
sudo apt-get install imagemagick