CarrierWave: create 1 uploader for multiple types of files
I came across this, and it looks like an example of how to solve this problem: https://gist.github.com/995663.
The uploader first gets loaded when you call mount_uploader
, at which point things like if image?
or elsif video?
won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead.
What the link I gave above does, is rewrite the process
method, so that it takes a list of file extensions, and processes only if your file matches one of those extensions
# create a new "process_extensions" method. It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
extensions = args.shift
args.each do |arg|
if arg.is_a?(Hash)
arg.each do |method, args|
processors.push([:process_trampoline, [extensions, method, args]])
end
else
processors.push([:process_trampoline, [extensions, arg, []]])
end
end
end
# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
extension = File.extname(original_filename).downcase
extension = extension[1..-1] if extension[0,1] == '.'
self.send(method, *args) if extensions.include?(extension)
end
You can then use this to call what used to be process
IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls)
def extension_white_list
IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end
process_extensions IMAGE_EXTENSIONS, :resize_to_fit => [1024, 768]
For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing. You'll have to change the version code to look like this:
version :big, :if => :image? do
process :resize_to_limit => [160, 100]
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
CarrierWave multiple file types validation with single uploader
I ran into the exact same use case:
In Asset.rb
Validate the format of the filename
validates :asset_file,
format:{
with: %r{\.(pdf|doc|png)$}i, message: "Wrong file format"
}
Use a regular expression to test the file name:
Here you can play with the regex:
http://rubular.com/r/Z3roRDDXAf
Hope this helps!
How to upload multiple files with carrierwave and create multiple records
You have to implement your requirement with has_many relation.
To save multiple record you may have parent child relationship just like below.
Ex :-
Post is a parent table and save its images as post_attachments which is child table
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
this is how you can achieve to save multiple files for parent record
For more detail see this Article which may be helpful to you.
https://kolosek.com/carrierwave-upload-multiple-images/
Carrierwave file upload with different file types
You should try using like this
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :process_image, if: :image?
process :process_pdf, if: :pdf?
protected
def image?(new_file)
new_file.content_type.start_with? 'image'
end
def pdf?(new_file)
new_file.content_type.start_with? 'application'
end
def process_image
# I process image here
end
def process_pdf
# I process pdf here
end
end
rails carrierwave, upload multiple files
you have two options
first option use a json
field in your db to upload the pictures. Not all db allow this, for example some mysql2
db do not allow you to use the json
type field in your db. Also using this is not the ruby on rails best practice
rails carrierwave, one user has many image
The other option is from this solution, you will not use a JSON field in the db, but create a separate table
Rails 4 multiple image or file upload using carrierwave
now please tell me why you do not want to use either this solutions, because I implemented both and I will try to help you
Also from your question it is not clear the issue you are having
How to upload two types of files into the same model using Carierwave in Rails?
It could be a strong parameters problem, where you are trying to access the value directly and it doesn't give you the value.
As it is an array or hash, treat it as a scalar value, as shown here. See if you have file information on
params.permit(submits: [])
or
params.require(:submits).permit!
Related Topics
Ruby Yaml Parser by Passing Constructor
Ruby No Implicit Conversion of Fixnum into String (Typeerror)
Linux Cli: How to Render Arabic Text into Bitmap
Heroku and @Font-Face - Embedded Fonts Wont Display on Heroku
Which Http Web Server How to Use to Debug Ruby Code Using Rubymine 3.0.1
Logger.Debug Not Writing to Log File in Rails
Setting Up .Emacs File for MAC Ruby Development
"Interfaces & Abstract Classes Unnecessary in Ruby" -> Can Someone Explain
How to Make Single Digit Number a Two Digit Number in Ruby
How to Print Unicode Character U-1F4A9 'Pile of Poo' Emoji
Ruby: How to Join Elements of an Array Together with a Prefix
E: Unable to Locate Package Heroku-Toolbelt
How to Associate a CSS/Sass Stylesheet to a View in Rails
What Ide/Editor Do You Use for Ruby on Linux
Why Are My Thread Variables Intermittent in Rails
Rails 3:Do I Need to Give Return True in a Before_Save Callback for an Object.Save to Work