Created
September 8, 2009 00:27
-
-
Save CamonZ/182638 to your computer and use it in GitHub Desktop.
Polymorphic Autosave Associations with Paperclip
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
= f.error_messages | |
%div{:style=>"padding-bottom:20px"} | |
%div | |
= f.label :name, 'Nombre del producto' | |
%br | |
= f.text_field :name, :size => 70 | |
#clientdesc | |
%div | |
= f.label :excerpt, 'Resumen del producto' | |
%br | |
= f.textile_editor :excerpt, :cols => 70 ,:rows => 8 | |
%br | |
%div | |
= f.label :description, 'Descripción del producto' | |
%br | |
= f.textile_editor :description, :cols => 70 ,:rows => 30 | |
%br | |
.image | |
Imagen del producto | |
%br | |
- f.fields_for :image do |image_form| | |
= image_form.file_field :data |
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 Image < ActiveRecord::Base | |
belongs_to :imageable, :polymorphic => true | |
has_attached_file :data, :styles => { | |
:small => ">150x150>", | |
:medium => "350x350>", | |
:gallery => "700x438" | |
} | |
validates_attachment_presence :data, :message => "Se necesita subir una imagen" | |
validates_attachment_content_type :data, { | |
:content_type => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png'] | |
} | |
def self.destroy_pics(imageable, images) | |
Image.find(images, :conditions => imageable).each(&:destroy) | |
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
#content | |
= javascript_include_tag 'textile-editor' | |
.inside | |
%h2 Agregar un nuevo producto | |
- form_for(@product, :html => {:multipart => true}) do |f| | |
= render :partial => 'form', :locals => {:f => f} | |
%p{:style=>"padding-top:20px"}= f.submit 'Crear Producto', :class => "button" | |
= textile_editor_initialize | |
= link_to 'Atras', products_path |
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 Product < ActiveRecord::Base | |
has_one :image, :as => :imageable, :autosave => true, :dependent => :destroy | |
acts_as_textiled :description, :excerpt | |
validates_presence_of :name, :description, :excerpt | |
accepts_nested_attributes_for :image, :allow_destroy => :true | |
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 ProductsController < ApplicationController | |
# GET /products/new | |
# GET /products/new.xml | |
def new | |
@product = Product.new | |
@product.image = Image.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @product } | |
end | |
end | |
# POST /products | |
# POST /products.xml | |
def create | |
params[:product][:image_attributes] ||= {} | |
@product = Product.new(params[:product]) | |
respond_to do |format| | |
if @product.save | |
flash[:notice] = 'El producto se ha creado exitosamente' | |
format.html { redirect_to(@product) } | |
format.xml { render :xml => @product, :status => :created, | |
:location => @product } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @product.errors, | |
:status => :unprocessable_entity } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment