问题
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