Created
March 30, 2011 23:10
-
-
Save andrellima/895496 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for (@gallery, :html => {:multipart => true}) do |f| %> | |
<% if @gallery.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@gallery.errors.count, "error") %> prohibited this gallery from being saved:</h2> | |
<ul> | |
<% @gallery.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> | |
<%= f.fields_for :gallery_photos do |gf_form| %> | |
<%= gf_form.label :photo, 'Photo:' %> | |
<%= gf_form.file_field :photo %> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GalleriesController < ApplicationController | |
# GET /galleries | |
# GET /galleries.xml | |
def index | |
@galleries = Gallery.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @galleries } | |
end | |
end | |
# GET /galleries/1 | |
# GET /galleries/1.xml | |
def show | |
@gallery = Gallery.find(params[:id]) | |
@gallery_photo = @gallery.gallery_photos.find(params[:id] ) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @gallery } | |
end | |
end | |
# GET /galleries/new | |
# GET /galleries/new.xml | |
def new | |
@gallery = Gallery.new | |
@gallery_photo = @gallery.gallery_photos.build(params[:gallery_photo] ) | |
3.times {@gallery.gallery_photos.build} # added this | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @gallery } | |
end | |
end | |
# GET /galleries/1/edit | |
def edit | |
@gallery = Gallery.find(params[:id]) | |
end | |
# POST /galleries | |
# POST /galleries.xml | |
def create | |
@gallery = Gallery.new(params[:gallery]) | |
@gallery_photo = @gallery.gallery_photos.build(params[:gallery_photo] ) | |
respond_to do |format| | |
if @gallery.save | |
format.html { redirect_to(@gallery, :notice => 'Gallery was successfully created.') } | |
format.xml { render :xml => @gallery, :status => :created, :location => @gallery } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @gallery.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /galleries/1 | |
# PUT /galleries/1.xml | |
def update | |
@gallery = Gallery.find(params[:id]) | |
respond_to do |format| | |
if @gallery.update_attributes(params[:gallery]) | |
format.html { redirect_to(@gallery, :notice => 'Gallery was successfully updated.') } | |
format.xml { head :ok } | |
else | |
format.html { render :action => "edit" } | |
format.xml { render :xml => @gallery.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /galleries/1 | |
# DELETE /galleries/1.xml | |
def destroy | |
@gallery = Gallery.find(params[:id]) | |
@gallery.destroy | |
respond_to do |format| | |
format.html { redirect_to(galleries_url) } | |
format.xml { head :ok } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gallery < ActiveRecord::Base | |
has_many :gallery_photos | |
accepts_nested_attributes_for :gallery_photos | |
attr_accessible :name | |
belongs_to :content | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GalleryPhoto < ActiveRecord::Base | |
belongs_to :gallery | |
has_attached_file :photo, | |
:styles => { :small => ["150", :png], :large => ["500", :png], :very_large => ['750x500>', :png] }, | |
:path => ":rails_root/public/images/:class/:attachment/:id/:style_:basename.png", | |
:url => "/images/:class/:attachment/:id/:style_:basename.png", | |
:default_url => "/images/sem_imagem.gif" | |
def geometry | |
geo = Paperclip::Geometry.from_file(photo.to_file(:large)) | |
ratio = geo.width/geo.height | |
if ratio > 1 | |
return 'horizontal' | |
else | |
return 'vertical' | |
end | |
end | |
def width | |
geo = Paperclip::Geometry.from_file(photo.to_file(:large)) | |
return geo.width | |
end | |
def height | |
geo = Paperclip::Geometry.from_file(photo.to_file(:large)) | |
return geo.height | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p id="notice"><%= notice %></p> | |
<p> | |
<b>Name:</b> | |
<%= @gallery.name %> | |
</p> | |
<%= image_tag @gallery_photo.photo.url(:thumb) %> | |
<%= link_to 'Edit', edit_gallery_path(@gallery) %> | | |
<%= link_to 'Back', galleries_path %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment