728x90 AdSpace

Latest Article

File and Picture / Image Uploading in Ruby on Rails

ruby Uploading a file in Rails File Uploading Using Rails  Uploading files and pictures into your Rails application  Ruby on Rails image uploads with CarrierWave in Windows 7 window 8 8.1 or later window 9 commands in dos

Following steps are required to see pictures

Step 1:   c:\>gem install carrierwave

Step 2:  c:\>gem install mini_magick


Step 3:  Add following link GemFile in your application

gem 'carrierwave'

gem 'mini_magick'

Step 4:  c:\>rails g uploader FilesUploader

Then you will see following code uncomments according to your requirement :

class FilesUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  #include CarrierWave::RMagick
   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

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  version :thumb do
 process :scale => [50, 50]

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
   def extension_white_list
    %w(jpg jpeg gif png)

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end


Step 5: add line in Model

class User < ActiveRecord::Base
 mount_uploader :image, PictureUploaderUploader

Step 6: Change following code in your views

image to image_url

<%= @user.image %>

If you want to url
<%= @user.image_url %>
If you want to show Image

<%= image_tag(@user.image_url) %>

Step 7: Change the following code also

<%= f.text_field :image %>
<%= f.file_field :image %>

File and Picture / Image Uploading in Ruby on Rails
  • Title : File and Picture / Image Uploading in Ruby on Rails
  • Posted by :
  • Date : 06:16
  • Labels :

  • Blogger Comments
  • Facebook Comments


Post a Comment