You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, you have succcessfully replaced the text field for country_code with a select drop down.
When the form is submitted, it'll still be saving the 2-letters country code in the database.
3. Add a Helper
On our application, instead of showing country_code, we want to show the country name.
Let's add a helper in ApplicatioHelper to do this:
module ApplicationHelper
def country_name(country_code)
country = ISO3166::Country[country_code]
if country
country.translations[I18n.locale.to_s]
else
country_code
end
end
end
This method uses the ISO3166::Country class from the country_select gem
to find a country instance based on the country_code,
and since I18n.locale.to_s returns en (our language) by default,
this helper method will return the English name for the country code.
4. Add Helper to index.html.erb and show.html.erb.
This will generate a new migration that adds the column image to the grams table.
Because you typed add_image_to_grams followed by the column and type image:string, then Rails can interpret that and generate the migration automatically for you.
If you open up the migration file, you would see add_column :grams, :image, string.
Now, run the migration with:
$ rake db:migrate
3. Create ImageUploader
Next, let's create the ImageUploader required by carrierwave to work.
Run this in your terminal:
$ rails generate uploader image
This will creat a new folder app/uploaders and a new file image_uploader.rb.
4. Update ImageUploader
Open up ImageUploader at app/uploaders/image_uploader.rb and change the contents to:
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
version :standard do
cloudinary_transformation width: 600, height: 600, crop: :pad, background: 'black'
end
end
This ImageUploader inherits from `CarrierWave::Uploader::Base` (carrierwave gem)
and includes methods from `Cloudinary::CarrierWave` (cloudinary gem).
This would take care of image uploads and processing of the image on Cloudinary.
5. Link the Gram model to ImageUploader
Remember we added a new column image to grams table?
That column should be managed by ImageUploader, so that after an image is uploaded,
the URL to the image would be saved into the new column.
Hence we need to link them up. Let's modif Gram.rb to look like so:
class Gram < ActiveRecord::Base
validates_presence_of :title, :content
mount_uploader :image, ImageUploader
end
6. Update new.html.erb
Next, we need to update our form so that you can upload an image when creating a gram.
Let's add the following to app/views/grams/new.html.erb, before the title input.
The :image field is for selecting and uploading an image,
and the :image_cache is for supporting page reloading and validation errors
while not losing the uploaded image.
7. Update show.html.erb
Add this line to your show.html.erb template to display the uploaded image:
To make all these work, we need to sign up for the Cloudinary service at http://cloudinary.com/.
Once you have signed in, download the YML file and put it at config/cloudinary.yml.
10. Start Your Rails Server
Finally, start your server with rails s -b 0.0.0.0 and give it a try!
Bonus: Protect Your Cloudinary Credentials
If you open up config/cloudinary.yml, you will see that it contains credentials to your Cloudinary account. In the real world, it's not considered a best practice.
Let's see how we can fix this.
First, let's add this to Gemfile, and then run bundle install.
group :development, :test do
gem 'dotenv-rails'
end
Next, create a .env file in the root folder of your project,
and copy the credentials in config/cloudinary.yml over.