Giter VIP home page Giter VIP logo

ruby_rails_cloudinary's Introduction

ruby_rails_cloudinary

Getting Started

We're going to create a simple photo app. You will be able to upload photos to a cloud and then pull the iamges back through a super fast CDN. We will be using two gems in our app 'carrierwave' & 'cloudinary'.

  • create simple ruby on rails app
  • install gem 'carrierwave', gem 'cloudinary'
  • upload photo
  • display photo through CDN to DOM

Cloudinary getting started guide

1. Create free cloudinary account

Sign-up for a free account. Create Account

2. Installation

To install the Cloudinary Ruby GEM to gemfile:

gem 'carrierwave'
gem 'cloudinary'

Run bundle

bundle install

Start off by generating an uploader:

rails generate uploader Picture

this should give you a file in:

app/uploaders/picture_uploader.rb

add to /uploaders/picture_uploader.rb

class PictureUploader < CarrierWave::Uploader::Base

include Cloudinary::CarrierWave

end

3. ActiveRecord

Add a string column to the model you want to mount the uploader by creating a migration: (alternatively you could plan to already have picture:string to your table upon first creation)

rails g migration add_picture_to_users picture:string
rake db:migrate

Open your model file and mount the uploader:

class User < ActiveRecord::Base
  mount_uploader :picture, PictureUploader
end

4. Upload images & Create users view

In our HTML form for Post editing, we add a File field for uploading the picture and also a hidden cache field for supporting page reloading and validation errors while not losing the uploaded picture.

<%= form_for @user do |f| %>
<%= f.file_field :picture %>
<%= f.hidden_field(:picture_cache) %>
<%= f.submit "Submit" %>
<% end %>

This image tag works with cloudinary gem to handle getting your images from the cloud. We will use this in the next step.

<%= cl_image_tag %>

in your views in '/app/views/users/index.html.erb' alternatively you could have a pictures views and paste this in there.

<section class="picture-layout">
  <header class="row bio-info">
    <section class="picture">
      <div class="col-sm-6 profile-1">
        <div class="picture-img">
          <%= cl_image_tag(@user.picture.url) %>
          
          <span style="vertical-align:middle">
            <%= form_for @user do |f| %>
            <%= f.file_field :picture %>
            <%= f.hidden_field(:picture_cache) %>
            <%= f.submit "Submit" %>
            <% end %>
          </span>
        </div>
      </div>

5. Configuration

Setting the configuration parameters can be done globally using a cloudinary.yml configuration file, located under the config directory of your Rails project.

create a new file cloudinary.yml, in your config directory. This is where we will be storing your API key and API secret, REMEMBER dont commit your API keys to gitHub.

touch/config/cloudinary.yml

next you will need to add ENV to hide your keys in cloudinary.yml like so =>

development:
  cloud_name: photo-app
  api_key: <%= ENV["cloud_key"] %>
  api_secret: <%= ENV["cloud_secret"] %>
  enhance_image_tag: true
  static_image_support: false
production:
  cloud_name: photo-app
  api_key: <%= ENV["cloud_key"] %>
  api_secret: <%= ENV["cloud_secret"] %>
  enhance_image_tag: true
  static_image_support: true
test:
  cloud_name: photo-app
  api_key: <%= ENV["cloud_key"] %>
  api_secret: <%= ENV["cloud_secret"] %>
  enhance_image_tag: true
  static_image_support: false

Now create a secrets.sh file in your app

touch /secrets.sh

in this folder add your API keys like so

#!/bin/bash
export cloud_key="123451277483254856795724365"
export cloud_secret="fdsgtvdfsgsdfhtdhwrt_RStdPJk"

in Terminal add using (I need to double check this information with Travis)

source 

5. Sample projects from Cloudinary Docs

Cloudinary Sample Projects

ruby_rails_cloudinary's People

Contributors

adamrobertreid avatar

Watchers

 avatar

Forkers

adamrobertreid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.