How to use paperclip with devise to add profile photo?

时光怂恿深爱的人放手 提交于 2019-12-12 04:19:07

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/35265665/how-to-use-paperclip-with-devise-to-add-profile-photo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!