public
Last active

A complete sample of how to perform nested polymorphic uploads in rails using carrierwave

  • Download Gist
_form.html.haml
Haml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
=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
dog.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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
dogs_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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
edit.html.haml
Haml
1 2 3 4 5 6 7 8
- title "Edit Dog"
 
= render 'form'
 
%p
= link_to "Show", @dog
|
= link_to "View All", admin_dogs_path
image_uploader.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 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
new.html.haml
Haml
1 2 3 4 5
- title "New Dog"
 
= render 'form'
 
%p= link_to "Back to List", dogs_path
picture.rb
Ruby
1 2 3 4 5 6
class Picture < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
attr_accessible :image, :attachable_id, :attachable_type
mount_uploader :image, ImageUploader
end
schema.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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

how do we get the gist download to work ?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.