Rails Image Upload: Using CarrierWave
1. In Rails, add it to your Gemfile:
gem 'carrierwave', '~> 2.0'
2. Start off by generating an uploader: rails generate uploader Avatar
3. this should give you a file in:
app/uploaders/avatar_uploader.rb
4. Check out this file for some hints on how you can customize your uploader.
It should look something like this:
class AvatarUploader < CarrierWave::Uploader::Base
storage :file
end
5. Add a string column to the model you want to mount the uploader by creating
a migration:
rails g migration add_avatar_to_users avatar:string
rails db:migrate
6. Open your model file and mount the uploader:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
7.
<div class="field col-lg-12 from-group">
<%= form.label :image %>
<%= form.file_field :image, :class => "form-control",:required=> true %>
</div>
8. <%= image_tag product.image.url(:thumb) %>
9. Add gem "mini_magick" in your gemfile.
link:= https://rubygems.org/gems/mini_magick/versions/4.5.1
When you modify the image size then you add this code in
uploaders/image_uploader.rb file.
version :thumb do
process resize_to_fill: [100,100]
end
version :medium do
process resize_to_fill: [400,400]
end
version :project do
process :resize_to_fill => [275, 180]
end
version :comment do
process :resize_to_fill => [580, 0]
end
version :projectimg do
process resize_to_fit: [275,180]
process :convert => :jpg
end
version :featuredmd do
process :resize_to_fill => [585, 350]
end
version :featuredsm do
process :resize_to_fill => [400, 350]
end
version :topicbanner do
process :resize_to_fill => [800, 350]
end
version :topicmd do
process :resize_to_fill => [450, 350]
end
version :topiclg do
process :resize_to_fill => [650, 350]
end
version :userbanner do
process :resize_to_fill => [800, 350]
end
No comments:
Post a Comment