Uploading a Song on Ruby on Rails

Read Time: half dozen mins Languages:

If you are building a web application, you lot definitely will want to enable paradigm uploading. Paradigm uploading is an of import feature in modernistic-day applications, and images have been known to be useful in search engine optimization.

In this tutorial (which is the first role of the Rails Epitome Uploading series), I will testify you how to enable image uploading in your Rails awarding using CarrierWave. Information technology will be a simple application equally the focus is on the image uploading.

CarrierWave is a Ruby gem that provides a simple and extremely flexible way to upload files from Ruby-red applications. Yous need to have Track on your machine to follow along. To be sure, open upwards your terminal and enter the control beneath:

That will bear witness you the version of Rails you have installed. For this tutorial I will exist using version iv.2.4, which you can install like so:

With that done, you are skilful to get.

Runway Awarding Setup

At present create a new Track project:

Open up your Gemfile and add the following gems.

The first gem is for CarrierWave, and the 2nd jewel chosen mini_magick helps with the resizing of images in your Rails awarding. With that washed, run package install.

Generate a scaffold resource to add CarrierWave'south functionality. Run the following command from your terminal:

A scaffold in Rails is a full set up of model, database migration for that model, controller to dispense information technology, views to view and manipulate the data, and a test suite for each of the above.

Drift your database next:

Setting Upwards CarrierWave

You need to create an initializer for CarrierWave, which will be used for loading CarrierWave after loading ActiveRecord.

Navigate to config > initializers and create a file: carrier_wave.rb.

Paste the code below into it.

From your terminal, generate an uploader:

This will create a new directory called uploaders in the app folder and a file inside called image_uploader.rb. The content of the file should await like this:

You can edit it to fit what you want. Let me have you through information technology.

Offset, uncomment the MiniMagick line. That should exist line seven.

You need this to generate unlike versions of an epitome. If you want to generate a thumbnail version of images uploaded, there is already a code form included in the image_uploader file for you lot. Uncomment the version code cake equally shown below:

You tin also add together unlike versions using the aforementioned format.

For the purpose of this tutorial, we will be saving to file and not fog. Fog is the Ruby cloud service library. I will show you how to put it into use in another part of this series. So go out your storage choice as it is.

For security purposes, certain files might pose a threat if allowed to be uploaded to the wrong location. CarrierWave allows you to specify a white-list of allowed extensions. You should see a method that looks similar what I have below, and so uncomment it.

It is time to mount your uploader. Navigate to your model and paste the code below.

Go to your views and edit it to look like what I have below:

Did you find this post useful?

oliverifest1961.blogspot.com

Source: https://code.tutsplus.com/tutorials/rails-image-upload-using-carrierwave-in-a-rails-app--cms-25183

0 Response to "Uploading a Song on Ruby on Rails"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel