Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class User < ActiveRecord::Base
has_one :photo, :dependent => :destroy
accepts_nested_attributes_for :photo, :allow_destroy => true
#using paperclip gem for user's images
class Photo < ActiveRecord::Base
belongs_to :user
has_attached_file :data, :styles => { :thumb=> "75x75#", :small => "100x100#"}, :whiny => false, :keep_old_files => false
validate :validates_account_avatar
def validates_account_avatar
allowed_mime_types = ["image/bmp", "image/gif", "image/jpeg", "image/png"]
max_size = 1.megabytes
errors[:format] << "Photo should be bmp, gif, jpeg or png." unless allowed_mime_types.include?(data.content_type.to_s)
errors[:size] << "Photo should be smaller than 1 Mb." if data.size > max_size
return true if self.errors.empty?
return false
#user form
<%= form_for @user, :html => {:multipart => true} do |f| %>
<% if @user.errors.any? %>
<p><%= pluralize(@user.errors.count, "error") %></strong> prohibited this user from being saved:</p>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
<%= f.text_field :name %>
<%= f.fields_for :photo do |photo| %>
<%= photo.file_field :data %>
<% end %>
<%=f.submit "Save changes", :class=>"btn btn-primary", "data-disable-with" => "wait..."%>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment