Skip to content

Instantly share code, notes, and snippets.

class Photo < ActiveRecord::Base
belongs_to :photo_gallery
has_attached_file :image, :styles => {
:medium => "300x300>",
:thumb => "100x100>"
},
:path => ":rails_root/app/assets/images/photos/:style/:filename"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
@fabrinal
fabrinal / gist:d51dd878bacf369bef6f
Last active August 29, 2015 14:22
Private scope part of photo_galleries_controller.rb
private
# Use callbacks to share common setup or constraints between actions.
def set_photo_gallery
@photo_gallery = PhotoGallery.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def photo_gallery_params
# add :photos param below
params.require(:photo_gallery).permit(:description,:photos)
@fabrinal
fabrinal / photo_galleries_controller.rb
Created June 1, 2015 12:39
Create method in photo_galleries_controller.rb
def create
@photo_gallery = PhotoGallery.new(photo_gallery_params)
respond_to do |format|
if @photo_gallery.save
# add save photos method in here
save_photos(params[:photos])
format.html { redirect_to @photo_gallery, notice: 'Photo gallery was successfully created.' }
format.json { render :show, status: :created, location: @photo_gallery }
else
format.html { render :new }
@fabrinal
fabrinal / photo_galleries_controller.rb
Last active August 29, 2015 14:22
add compare method after update method executed
def update
respond_to do |format|
if @photo_gallery.update(photo_gallery_params)
# add compare method in here
compare_photos(params[:photos])
format.html { redirect_to @photo_gallery, notice: 'Photo gallery was successfully updated.' }
format.json { render :show, status: :ok, location: @photo_gallery }
else
format.html { render :edit }
format.json { render json: @photo_gallery.errors, status: :unprocessable_entity }
@fabrinal
fabrinal / _form.html.erb
Created June 1, 2015 12:47
Form sample code to do multiple photos upload inside photo gallery form
<%= form_for(@photo_gallery,:html => {multipart: true}) do |f| %>
<% if @photo_gallery.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@photo_gallery.errors.count, "error") %> prohibited this photo_gallery from being saved:</h2>
<ul>
<% @photo_gallery.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
@fabrinal
fabrinal / _form.html.erb
Created June 1, 2015 12:50
Add one link to delete photo using ajax
<div class="field">
<%= f.label 'Photos' %><br>
<% 10.times do |i| %>
Photo <%= i + 1 %> &nbsp;
<%= file_field_tag "photos[#{i}]", accept: 'image/png,image/jpeg' %> &nbsp;
<%= @photo_gallery.photos[i].nil? ? '' : label_tag(@photo_gallery.photos[i].image_file_name,nil,id: "photo#{@photo_gallery.photos[i].id}") %> &nbsp;
<!-- add link to delete single photo object below -->
<%= @photo_gallery.photos[i].nil? ? '' : link_to('Hapus Foto',@photo_gallery.photos[i],remote: true, method: :delete,:class => "delete_photo", data: {value: @photo_gallery.photos[i].id}) %><br><br>
<% end %>
</div>
@fabrinal
fabrinal / photo_galleries.coffee
Created June 1, 2015 12:54
Add ajax to delete single photo
$(document).ready ->
$('.delete_photo').each ->
$(this).click ->
if confirm("Are you sure to delete photo?")
photo_id = $(this).attr('data-value')
$(this).on "ajax:success",() ->
alert("Photo deleted successfully")
$("a[data-value="+photo_id+"]").remove()
$("#photo"+photo_id).remove()
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body {
height: 100%;
margin: 0;
<div id="map"></div>
<script>
function initMap() {
var myLatLng = {lat: 1.12321, lng: 160.123232};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: myLatLng
});
}
</script>
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});