Skip to content

Instantly share code, notes, and snippets.

@gentamura
Created December 6, 2014 03:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gentamura/a0400760c33f8352281c to your computer and use it in GitHub Desktop.
Save gentamura/a0400760c33f8352281c to your computer and use it in GitHub Desktop.
Cloudinary + Carrierwave + Heroku + Railsでの画像を手軽に利用する方法 ref: http://qiita.com/GenTamura84/items/38cf899827bba050a21c
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= image_tag(@user.avatar_url(:thumbnail), :width => 80, :height => 80) %>
<%= f.file_field :avatar %>
<%= f.check_box :remove_avatar %> 画像を削除する
<%= f.hidden_field :avatar_cache %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
process :convert => 'png'
process :tags => ['avatar']
version :standard do
process :resize_to_fill => [100, 150, :north]
end
version :thumbnail do
process :resize_to_fit => [50, 50]
end
def public_id
return model.id ・・・※1
end
end
$heroku open
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'carrierwave'
gem 'cloudinary'
gem 'heroku'
group :test, :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'thin'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
CloudinaryDemo::Application.routes.draw do
resources :users
root :to => 'users#index'
end
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @user.name %>
</p>
<p>
<b>Avatar:</b>
<%= image_tag(@user.avatar_url) %>
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
class User < ActiveRecord::Base
attr_accessible :avatar, :avatar_cache, :name, :remove_avatar
mount_uploader :avatar, AvatarUploader
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment