Skip to content

Instantly share code, notes, and snippets.

@blizzarddreams
Created February 17, 2015 02:12
Show Gist options
  • Select an option

  • Save blizzarddreams/27d7f7b22ecbf9bddb3d to your computer and use it in GitHub Desktop.

Select an option

Save blizzarddreams/27d7f7b22ecbf9bddb3d to your computer and use it in GitHub Desktop.
require 'securerandom'
class Image < ActiveRecord::Base
validates :title, presence: true
validates :image, presence: true
extend FriendlyId
friendly_id :random_id
belongs_to :user
mount_uploader :image, ImageUploader
before_save :random_string
def random_string
self.random_id = SecureRandom.urlsafe_base64(6)
self.user_id = current_user.id
end
end
class ImagesController < ApplicationController
def new
@image = Image.new
end
def create
@image = Image.new(image_params)
if @image.save
redirect_to image_path(@image.id)
else
redirect_to 'new'
end
end
def show
@image = Image.friendly.find(params[:id])
end
private
def image_params
params.require(:image).permit(:user_id, :title, :image, :random_id)
end
end
<% if @image.errors.any? %>
<div id='error_explanation'>
<h2><%= pluralize(@image.errors.count, "error") %></h2>
<ul>
<% @image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for @image, :html => {:multipart => true} do |f| %>
<%= f.label :title, :class =>'textfield' %><br />
<%= f.text_field :title, :class =>'textfield' %><br />
<%= f.file_field :image %><br />
<!--<button id='custom_button'>Browse</button>-->
<%= f.submit "Upload" %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment