Jeramae Bohol February 2016

How to use paperclip with devise to add profile photo?

Creating a pinterest-like app where users can create a pin with an image, title, and description is easy with paperclip.

But I'm having trouble modifying the paperclip docs when I want to use it as a way to upload a profile picture when I'm using devise.

Any tips?

Answers


Dheeresha February 2016

Try the following code.

In your users/registrations/new.html.erb view file add the following code.

<%= form_for @user, url: users_path, html: { multipart: true } do |f| %>
  <%= f.file_field :avatar %>
<% end %>

In your devise's user model Add the following code.

class User < ActiveRecord::Base

  has_attached_file :avatar, :styles => { :large => "400x400>", :medium => "200x200>", :thumb => "100x100>" }

  validates_attachment :avatar, :presence => true, :content_type => { :content_type => "image/jpeg", :message => "Only JPEG formats allowed" }

end

In your controller.

def create
  @user = User.create( user_params )
end


private

def user_params
  params.require(:user).permit(:avatar)
end

Post Status

Asked in February 2016
Viewed 1,286 times
Voted 8
Answered 1 times

Search




Leave an answer