Pregunta

I am working on adding a profile picture to the User model in my Rails app. I've already gotten screenshots successfully working with another model, but for some reason I'm having a lot of difficulties with profile pictures. To handle profile pictures, I've created a new ProfilePics model:

class ProfilePic < ActiveRecord::Base
  attr_accessible :image, :user_id

  has_attached_file :profile_pic, :default_url => "/system/user_profile_pics/profile.png",
                                  :url => "/system/user_profile_pics/:id/:basename.:extension",
                                  :path => ':rails_root/public:url'
                                  :styles => { :large => "800x400", :thumb => "36x36" }

  # **** Associations ****

  # State that each profile picture can have an associated user
  belongs_to :users

  # **** Validations ****

  # Only allow the user to upload .bmp, .gif, .jpg, .jpeg, and .png files
  validates_attachment_content_type :image, :content_type => /^image\/(bmp|gif|jpg|jpeg|png)/

  # Validate the presence of the user id
  validates :user_id, :presence => true

  # Order all profile pictures by ID, from first to last
  default_scope :order => 'profile_pics.id ASC'
end

When a user signs up, he/she should be set the default profile picture. This picture is the image file specified in the :default_url argument for the has_attached_file method. However, I can't seem to figure out how to assign the user the default profile picture in the controller, after the User has been created. I don't want to add the profile picture to the sign up form, and if I just omit it from the controller, I receive the following error message:

undefined method `before_image_post_process'

I haven't made the profile picture a requirement on user creation. I believe I have all of the correct database tables set up, but for some reason I keep getting this error. Here's my attempt at assigning the user the default profile picture in the controller:

if @user.save
  # Create a profile picture for the user
  @user.profile_pic = ProfilePic.new(:image => nil, :user_id => @user.id)

  ...
end

When debugging, immediately after saving the user, typing "@user.profile_pic" in the console returns the same 'before_image_post_process' error.

Does anyone have any insight on this issue? Thank you very much in advance!

¿Fue útil?

Solución

You are getting this error because you defined the attached file attribute as profile_pic but you are doing the Paperclip validation on the image attribute.

When you define a has_attached_file attribute, Paperclip automatically creates a <name>_post_process callback which it uses later in the validation (where is the name of the has_attached_file attribute).

You created profile_pic_post_process but then the validation is looking for image_post_process, hence the error.

Change your validation line in the ProfilePic model:

  # Only allow the user to upload .bmp, .gif, .jpg, .jpeg, and .png files
  validates_attachment_content_type :profile_pic, :content_type => /^image\/(bmp|gif|jpg|jpeg|png)/
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top