Javascript Image Upload Ajax Tutorial Rails Carrierwave

Read Time: 11 mins Languages:

This is another article in the "Uploading with Rails" series. Today we are going to run into Carrierwave—ane of the virtually popular file uploading solutions for Rails. I like Carrierwave because it is easy to get started, it has lots of features out of the box, and information technology provides dozens of "how to" articles written past the members of the community, so yous won't become lost.

In this commodity, yous will learn how to:

  • Integrate Carrierwave into your Rails app
  • Add validations
  • Persist files across requests
  • Remove files
  • Generate thumbnails
  • Upload files from remote locations
  • Innovate multiple file uploads
  • Add back up for cloud storage

The source lawmaking for this commodity is available on GitHub. Enjoy reading!

Laying the Foundations

As always, start by creating a new Rails awarding:

For this demo I'll exist using Rails v.0.2. Delight note that Carrierwave one supports only Rails 4+ and Ruby 2. If you are still riding on Runway 3, and then hook up Carrierwave version 0.xi.

To run across Carrierwave in action, nosotros are going to create a very simple blogging application with a sole Postal service model. It will have the following master attributes:

  • title (string)
  • trunk (text)
  • paradigm (string)—this field is going to contain an image (a file's name, to be precise) attached to the post

Generate and apply a new migration:

Fix some routes:

config/routes.rb

Also, create a very basic controller:

posts_controller.rb

At present permit's craft the alphabetize view:

views/posts/alphabetize.html.erb

And the respective partial:

views/posts/_post.html.erb

Here I am using the Runwaytruncate method to display only the first 150 symbols from the post. Before we create other views and a form partial, let'due south firstly integrate Carrierwave into the application.

Integrating Carrierwave

Driblet in a new gem into the Gemfile:

Gemfile

Run:

Carrierwave stores its configuration withinuploadersthat are included into your models. To generate an uploader, use the following command:

At present, within app/uploaders, you lot volition find a new file called image_uploader.rb. Note that it has some useful comments and examples, so you may use it to become started. In this demo we will use ActiveRecord, but Carrierwave also has support for Mongoid, Sequel, and DataMapper.

Next, we need to include or mount this uploader into the model:

models/mail service.rb

The uploader already has sane default settings, but at the very least nosotros need to choose where the uploaded files will be stored. For now, allow'due south utilize file storage:

uploaders/image_uploader.rb

Past default, files will be placed inside the public/uploads directory, so it is best to exclude it from the version control system:

.gitignore

You may also alter thestore_dir method within your uploader to cull some other location.

At this point, we tin can create a new view and a form fractional to start uploading files:

views/posts/new.html.erb

views/posts/_form.html.erb

Note that the PostsController does non need to be modified equally we already permitted the paradigm attribute.

Lastly, create the edit view:

views/posts/edit.html.erb

That's it! You may kicking the server and try to create a mail service with an prototype. The problem is that this prototype is not visible anywhere, so let's proceed to the side by side department and add a bear witness page!

Displaying Images

And so, the only view we accept not created yet is bear witness. Add it now:

views/posts/show.html.erb

As you tin can see, displaying an attachment is actually easy: all you need to do is say @mail service.image.url to take hold of an epitome's URL. To get a path to the file, use thecurrent_path method. Annotation that Carrierwave likewise provides an epitome? method for u.s.a. to check whether an attachment is present at all (the image method itself will never render zilch, even if the file is not present).

At present, after navigating to a mail, yous should run into an image, simply it might appear likewise large: later on all, we are not restricting dimensions anywhere. Of grade, we could accept scaled the image downwards with some CSS rules, but it is much better to generate a thumbnail afterwards the file has been uploaded. This, notwithstanding, requires some additional steps.

Generating Thumbnails

In gild to crop and scale images, we need a separate tool. Out of the box Carrierwave has support for RMagick and MiniMagick gems that, in turn, are used to manipulate images with the help of ImageMagick. ImageMagick is an open-source solution allowing you to edit existing images and generate new ones, then earlier proceeding you demand to download and install it. Next, you are free to option either of the two gems. I'll stick with MiniMagick, because information technology is much easier to install and it has better support:

Gemfile

Run:

Then include MiniMagick into your uploader:

uploaders/image_uploader.rb

At present we simply need to introduce a new version to our uploader. The concept of versions (or styles) is used in many file uploading libraries; it just means that additional files based on the original attachment will be created with, for example, different dimensions or formats. Innovate a new version called thumb:

uploaders/image_uploader.rb

You may have as many versions as you lot like and, what'due south more, versions can even be built on top of other ones:

uploaders/image_uploader.rb

If you accept already uploaded some images, they won't have thumbnails available. This is not a problem, though, equally you can copy them from the Rail console:

Lastly, display your thumbnail with a link to the original image:

views/posts/show.html.erb

Boot the server and observe the result!

Calculation Validations

Currently our uploading works, but we're not validating user input at all, which is, of course, bad. As long as we desire to work only with images, permit's whitelist .png, .jpg and .gif extensions:

uploaders/image_uploader.rb

You may also add content type checks past defining a content_type_whitelist method:

uploaders/image_uploader.rb

Alternatively, it is possible to blacklist some file types, for example executables, by defining thecontent_type_blacklist method.

Apart from checking a file'south type and extension, allow'southward enforce it to be less than 1 megabyte. To do it, we'll crave an additional jewel supporting file validations for ActiveModel:

Gemfile

Install it:

At present introduce the desired validations (notation that I am besides adding checks for the title and body attributes):

models/post.rb

The adjacent thing to do is to add I18n translations for Carrierwave's error messages:

config/locales/en.yml

Currently, we do non brandish validation errors anywhere, so allow'south create a shared fractional:

views/shared/_errors.html.erb

Utilise this fractional inside the grade:

views/posts/_form.html.erb

At present try to upload some invalid files and observe the result. It should piece of work, simply if you cull a valid file and do not fill in the title or body, and then the checks volition still fail and an error volition be displayed. Withal, the file field will be cleared out and the user will need to choose the image again, which is not very convenient. To set it, we demand to add another field to the form.

Persisting Files Across Requests

Persisting files beyond form redisplays is actually quite easy. All you need to do is add a new hidden field and permit it inside the controller:

views/shared/_form.html.erb

posts_controller.rb

Now the image_cache will be populated automatically and the epitome won't be lost. It may be helpful to display a thumbnail too so that user understands the epitome was processed successfully:

views/shared/_form.html.erb

Removing Images

Some other very mutual characteristic is the power to remove attached files when editing a record. With Carrierwave, implementing this feature is not a problem. Add a new checkbox to the form:

views/shared/_form.html.erb

And permit the remove_image aspect:

posts_controller.rb

That's it! To remove an image manually, use theremove_image! method:

Uploading From a Remote Location

Carrierwave also provides a very absurd feature out of the box: the ability to upload files from remote locations by their URL. Permit's introduce this ability now by adding a new field and permitting the corresponding aspect:

views/shared/_form.html.erb

posts_controller.rb

How cool is that? Y'all don't need to brand any changes at all, and you can test this characteristic right away!

Working With Multiple Uploads

Suppose we desire our mail service to accept multiple attachments bachelor. With the current setup it is non possible, but luckily, Carrierwave supports such a scenario too. To implement this feature, you demand to add either a serialized field (for SQLite) or a JSON field (for Postgres or MySQL). I prefer the latter option, so let's switch to a new database adapter now. Remove the sqlite3 gem from the Gemfile and add pg instead:

Gemfile

Install it:

Modify the database configuration like this:

config/database.yml

Create the corresponding Postgres database, and so generate and utilise the migration:

If you lot prefer to stick with SQLite, follow the instructions listed in Carrierwave'due south documentation.

Now mount the uploaders (note the plural form!):

model/mail service.rb

I am using the same uploader for attachments, just of course y'all tin generate a new i with a different configuration.

Add together the multiple file field to your form:

views/shared/_form.html.erb

As long as the attachments field is going to contain an array, it should be permitted in the following way:

posts_controller.rb

Lastly, you may iterate over the post's attachments and display them as usual:

views/shared/evidence.html.erb

Note that each attachment is going to have a thumbnail as configured in our ImageUploader. Squeamish!

Using Cloud Storage

Sticking with file storage is not ever convenient and/or possible every bit, for example, on Heroku it is not possible to store custom files. Therefore you lot might ask how to marry Carrierwave with Amazon S3 cloud storage? Well, that's a pretty easy task as well. Carrierwave depends on the fog-aws gem to implement this characteristic:

Gemfile

Install it:

Let'due south create an initializer for Carrierwave and configure the cloud storage globally:

config/initializers/carrierwave.rb

There are some other options available, which can be plant in the documentation.

I am using the dotenv-rail gem to set the environment variables in a secure style, just you lot may choose any other selection. Even so, make sure that your S3 key pair is non bachelor publicly, because otherwise anyone tin can upload anything to your bucket!

Next, replace the storage :file line with:

uploaders/image_uploader.rb

Apart from S3, Carrierwave supports uploads to Google Storage and Rackspace. These services are piece of cake to prepare up also.

Conclusion

This is it for today! We have covered all the major features of Carrierwave, and now you can start using information technology in your projects. Information technology has some boosted options bachelor, so do browse the documentation.

If y'all are stuck, don't hesitate to mail your questions. Also, it might be useful to take a peek into Carrierwave's wiki, which hosts useful "how to" articles answering many mutual questions.

So I thank you for staying with me, and happy coding!

Did you lot find this post useful?

utherheltaked.blogspot.com

Source: https://code.tutsplus.com/articles/uploading-with-rails-and-carrierwave--cms-28409

0 Response to "Javascript Image Upload Ajax Tutorial Rails Carrierwave"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel