2016-02-08 31 views
0

創建一個pinterest般的應用程序,用戶可以通過回形針輕鬆創建包含圖像,標題和描述的圖釘。如何在設計中使用回形針添加個人資料照片?

但是我在修改回形針文檔時遇到了問題,當我想用​​它作爲上傳配置文件圖片的方式時,我正在使用設計。

任何提示?

+2

我認爲[本博客](http://www.peoplecancode.com/tutorials/users-avatars-uploading-images-using-paperclip)將幫助你。 – SpunkyLive

+1

@SpunkyLive感謝隊友!這對我有用:) –

回答

1

請嘗試下面的代碼。

在您的users/registrations/new.html.erb視圖文件中添加以下代碼。

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

在您的設計的用戶模型中添加以下代碼。

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 

在你的控制器中。

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


private 

def user_params 
    params.require(:user).permit(:avatar) 
end 
相關問題