Saturday, 14 September 2013

How to choose a specific paperclip image to be a "profile pic" for an object

How to choose a specific paperclip image to be a "profile pic" for an object

I currently have a Dog model which I have a has many association with my
asset model. My asset model is used for storing paperclip images in S3. I
would like to be able to choose one of the Dog's pictures to be the
"profile pic" for that dog. Right now I'm not sure how to do this.
app/models/dog.rb
class Dog < ActiveRecord::Base
attr_accessible :name, :description, :age, :protection,
:assets_attributes, :dog_titles_attributes, :dog_parents_attributes,
:movies_attributes, :profile_pic
has_many :assets
has_many :movies
has_one :profile_pic
has_many :dog_parents, :dependent => :destroy
has_many :parents, through: :dog_parents
has_many :dog_titles, :dependent => :destroy
has_many :titles, through: :dog_titles
accepts_nested_attributes_for :dog_titles, :allow_destroy => true
accepts_nested_attributes_for :dog_parents, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true
accepts_nested_attributes_for :movies, :allow_destroy => true
validates_associated :assets
validates_uniqueness_of :name, case_sensitive: false
validates_presence_of :name
end
app/models/asset.rb
class Asset < ActiveRecord::Base
attr_accessible :dog_id, :parent_id, :litter_id, :asset, :category,
:caption, :_destroy
belongs_to :dog
belongs_to :parent
belongs_to :litter
has_attached_file :asset, :styles => {
large: "500x500>",
medium: "300x300#",
thumb: "100x100#",
square: '200x200#' }
validates_attachment_size :asset, :less_than => 5.megabytes
validates_attachment_content_type :asset, :content_type =>
['image/jpeg', 'image/png', 'image/jpg']
end
app/controllers/dogs_controller.rb
class DogsController < ApplicationController
before_filter :ensure_active_user, only: [:new, :create, :edit, :update,
:destroy]
def index
@dogs = Dog.find(:all)
end
def show
@dog = Dog.find(params[:id])
end
def new
@dog = Dog.new
@titles = Title.all
@parents = Parent.all
end
def create
@dog = Dog.new(params[:dog])
@titles = Title.all
respond_to do |format|
if @dog.save
format.html { redirect_to action: "index", notice: 'Dog was
successfully created.' }
else
format.html { render action: "new" }
end
end
end
def edit
@dog = Dog.find(params[:id])
@titles = Title.all
@parents = Parent.all
@assets = @dog.assets
end
def update
@dog = Dog.find(params[:id])
respond_to do |format|
if @dog.update_attributes(params[:dog])
format.html { redirect_to action: "index", notice: 'Dog updated.' }
else
format.html { render action: "edit" }
end
end
end
def destroy
dog = Dog.find(params[:id])
respond_to do |format|
if dog.destroy
format.html { redirect_to action: "index", notice: 'Dog updated.' }
else
format.html { redirect_to action: "index", error: "Delete wasn't
successful"}
end
end
end
end
app/views/dogs/edit.html.haml
= simple_form_for @dog, html: { multipart: true } do |f|
= f.input :name, input_html: { style: 'width:400px' }
= f.input :description, input_html: { style: 'width:600px; height:200px' }
= f.input :age, label: "Date of Birth",
input_html: { style: 'width:205px'},
as: :date, start_year: Date.today.year - 20,
end_year: Date.today.year,
order: [:day, :month, :year]
#picture-form-edit
%h4 Pictures
%ul
= f.simple_fields_for :assets do |p|
%li
= render 'asset_fields', :f => p
%p= link_to_add_fields "Add Photo", f, :assets
#movie-form-edit
%h4 Movies
%ul
= f.simple_fields_for :movies do |p|
%li
= render 'movie_fields', :f => p
%p= link_to_add_fields "Add Movie", f, :movies
#achievements-form-edit
%h4 Dog titles and achievements
= f.simple_fields_for :dog_titles do |p|
= render 'dog_title_fields', :f => p
%p= link_to_add_fields "Add Dog Titles", f, :dog_titles
#parents-form-edit
%h4 Parents
= f.simple_fields_for :dog_parents do |p|
= render 'dog_parent_fields', :f => p
%p= link_to_add_fields "Add Dog parents", f, :dog_parents
#submit-form
%p= f.submit "Edit Dog", class: "btn btn-primary"
app/views/dogs/_asset_fields.html.haml
%p.fields
- if f.object.asset?
= image_tag(f.object.asset.url(:thumb))
- else
= f.file_field :asset
= f.hidden_field :_destroy
= link_to_function "remove", "remove_fields(this)"
Additional I have tried adding a collection to my edit.html.haml to let
the user choose from the list of dogs, but this doesn't show images and
rather it just shows the image id.
Example:
%h4 Profile Pic
= f.collection_select :profile_pic, @assets, :id, :asset
What I want to do is let the user pick from the current pictures and
select one as their profile picture. I have been told using check boxes
with javascript would be a good way to do this, but I'm not sure how it
would be implemented. Would I add a check box to my
_asset_fields.html.haml?

No comments:

Post a Comment