Skip to content

Instantly share code, notes, and snippets.

@mhenrixon
Created May 18, 2011 11:02
Show Gist options
  • Save mhenrixon/978371 to your computer and use it in GitHub Desktop.
Save mhenrixon/978371 to your computer and use it in GitHub Desktop.
A complete sample of how to perform nested polymorphic uploads in rails using carrierwave
=semantic_form_for [:admin, @dog], validate: true, html: {multipart: true} do |f|
=f.inputs do
=f.input :name
=f.input :kennel_name
=f.input :birthdate
=f.input :gender, as: :radio, collection: {'Tik' => 'F', 'Hane' => 'M'}
=f.input :father_id, as: :select, collection: @dogs
=f.input :mother_id, as: :select, collection: @bitches
=f.semantic_fields_for :pictures do |pic|
=pic.inputs do
=pic.hidden_field :id
=pic.hidden_field :attachable_id
=pic.hidden_field :attachable_type
=pic.file_field :image
=f.buttons
class Dog < ActiveRecord::Base
attr_accessible :name, :kennel_name, :mother_id, :father_id, :birthdate, :gender, :pictures_attributes, :pictures
attr_accessor :image, :file
belongs_to :father, class_name: "Dog", foreign_key: "father_id"
belongs_to :mother, class_name: "Dog", foreign_key: "mother_id"
has_many :pictures, as: :attachable
accepts_nested_attributes_for :pictures
validates :gender, inclusion: ['F', 'M']
def gender_name
return 'Tik' if self.gender == 'F'
return 'Hane' if self.gender == 'M'
end
def self.with_parents
includes(:father).includes(:mother)
end
def self.with_pictures
includes(:pictures)
end
end
class DogsController < ApplicationController
before_filter :login_required
def index
@dogs = Dog.with_parents
end
def show
@dog = Dog.with_parents.with_pictures.find(params[:id])
@puppies = Dog.where('father_id = ? OR mother_id = ?', params[:id], params[:id])
end
def new
@dog = Dog.new
@dog.pictures.build
@bitches = Dog.where('gender = ?', 'F')
@dogs = Dog.where('gender = ?', 'M')
end
def create
@dog = Dog.new(params[:dog])
if @dog.save
redirect_to [:admin, @dog], :notice => "Successfully created dog."
else
render :action => 'new'
end
end
def edit
@dog = Dog.find(params[:id])
@dog.pictures.build if @dog.pictures.empty?
@bitches = Dog.where('id != ?', params[:id]).where('gender = ?', 'F')
@dogs = Dog.where('id != ?', params[:id]).where('gender = ?', 'M')
end
def update
@dog = Dog.find(params[:id])
if @dog.update_attributes(params[:dog])
redirect_to [:admin, @dog], :notice => "Successfully updated dog."
else
render :action => 'edit'
end
end
def destroy
@dog = Dog.find(params[:id])
@dog.destroy
redirect_to admin_dogs_url, :notice => "Successfully destroyed dog."
end
end
- title "Edit Dog"
= render 'form'
%p
= link_to "Show", @dog
|
= link_to "View All", admin_dogs_path
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process convert: 'png', :resize_to_fit => [1024, 1024]
version :medium do
process resize_to_fit: [300, 300]
process convert: 'png'
end
version :thumb do
process resize_to_fit: [100, 100]
process convert: 'png'
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
- title "New Dog"
= render 'form'
%p= link_to "Back to List", dogs_path
class Picture < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
attr_accessible :image, :attachable_id, :attachable_type
mount_uploader :image, ImageUploader
end
ActiveRecord::Schema.define(:version => 20110508171542) do
create_table "dogs", :force => true do |t|
t.string "name"
t.string "kennel_name"
t.integer "mother_id"
t.integer "father_id"
t.date "birthdate"
t.string "gender", :default => "F"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pictures", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "image"
t.integer "attachable_id"
t.string "attachable_type"
end
end
@wlgithub
Copy link

wlgithub commented Aug 8, 2013

how do we get the gist download to work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment