Method: CarrierWave::Mount#mount_uploader

Defined in:
lib/carrierwave/mount.rb

#mount_uploader(column, uploader = nil, options = {}, &block) ⇒ Object

Mounts the given uploader on the given column. This means that assigning and reading from the column will upload and retrieve files. Supposing that a User class has an uploader mounted on image, you can assign and retrieve files like this:

@user.image # => <Uploader>
@user.image.store!(some_file_object)

@user.image.url # => '/some_url.png'

It is also possible (but not recommended) to omit the uploader, which will create an anonymous uploader class.

Passing a block makes it possible to customize the uploader. This can be convenient for brevity, but if there is any significant logic in the uploader, you should do the right thing and have it in its own file.

Added instance methods

Supposing a class has used mount_uploader to mount an uploader on a column named image, in that case the following methods will be added to the class:

[image] Returns an instance of the uploader only if anything has been uploaded [image=] Caches the given file

[image_url] Returns the url to the uploaded file

[image_cache] Returns a string that identifies the cache location of the file [image_cache=] Retrieves the file from the cache based on the given cache name

[remote_image_url] Returns previously cached remote url [remote_image_url=] Retrieve the file from the remote url

[remove_image] An attribute reader that can be used with a checkbox to mark a file for removal [remove_image=] An attribute writer that can be used with a checkbox to mark a file for removal [remove_image?] Whether the file should be removed when store_image! is called.

[store_image!] Stores a file that has been assigned with image= [remove_image!] Removes the uploaded file from the filesystem.

[image_integrity_error] Returns an error object if the last file to be assigned caused an integrity error [image_processing_error] Returns an error object if the last file to be assigned caused a processing error [image_download_error] Returns an error object if the last file to be remotely assigned caused a download error

[image_identifier] Reads out the identifier of the file

Parameters

[column (Symbol)] the attribute to mount this uploader on [uploader (CarrierWave::Uploader)] the uploader class to mount [options (Hash=> Object)] a set of options [&block (Proc)] customize anonymous uploaders

Options

[:mount_on => Symbol] if the name of the column to be serialized to differs you can override it using this option [:ignore_integrity_errors => Boolean] if set to true, integrity errors will result in caching failing silently [:ignore_processing_errors => Boolean] if set to true, processing errors will result in caching failing silently

Examples

Mounting uploaders on different columns.

class Song
  mount_uploader :lyrics, LyricsUploader
  mount_uploader :alternative_lyrics, LyricsUploader
  mount_uploader :file, SongUploader
end

This will add an anonymous uploader with only the default settings:

class Data
  mount_uploader :csv
end

this will add an anonymous uploader overriding the store_dir:

class Product
  mount_uploader :blueprint do
    def store_dir
      'blueprints'
    end
  end
end


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/carrierwave/mount.rb', line 134

def mount_uploader(column, uploader=nil, options={}, &block)
  mount_base(column, uploader, options.merge(multiple: false), &block)

  mod = Module.new
  include mod
  mod.class_eval "\n    def \#{column}\n      _mounter(:\#{column}).uploaders[0] ||= _mounter(:\#{column}).blank_uploader\n    end\n\n    def \#{column}=(new_file)\n      _mounter(:\#{column}).cache([new_file])\n    end\n\n    def \#{column}_url(*args)\n      \#{column}.url(*args)\n    end\n\n    def \#{column}_cache\n      _mounter(:\#{column}).cache_names[0]\n    end\n\n    def \#{column}_cache=(cache_name)\n      _mounter(:\#{column}).cache_names = [cache_name]\n    end\n\n    def remote_\#{column}_url\n      [_mounter(:\#{column}).remote_urls].flatten[0]\n    end\n\n    def remote_\#{column}_url=(url)\n      _mounter(:\#{column}).remote_urls = url\n    end\n\n    def remote_\#{column}_request_header=(header)\n      _mounter(:\#{column}).remote_request_headers = [header]\n    end\n\n    def \#{column}_identifier\n      _mounter(:\#{column}).read_identifiers[0]\n    end\n\n    def \#{column}_integrity_error\n      \#{column}_integrity_errors.last\n    end\n\n    def \#{column}_processing_error\n      \#{column}_processing_errors.last\n    end\n\n    def \#{column}_download_error\n      \#{column}_download_errors.last\n    end\n  RUBY\nend\n", __FILE__, __LINE__+1