2014-04-03 65 views
3

所以,目前我有一個問題,獲取個人資料頁面的頭像和個人資料封面圖片的URL。Heroku + Rails 4 +回形針瓦特/ AWS S3網址不工作

有趣的是,它會顯示如果我使用:

current_user.avatar.url(:thumb) 

# it will not show if I use: 
@user.avatar.url(:thumb) 

# production.rb paperclip config 
config.paperclip_defaults = { 
:url => ":s3_domain_url", 
:path => "/:class/:attachment/:id/:style/:filename", 
:storage => :s3, 
:s3_credentials => { 
    :bucket => ENV['S3_BUCKET_NAME'], 
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'], 
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] 
    } 
} 

# user.rb model 
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" } 
has_attached_file :cover_image, :styles => { :large => "900x900#" } 

# Using devise so this is all I have for the user. It handles editing and everything else. 
# users_controller.rb 
class UsersController < ApplicationController 

    def show 
    @user = User.find_by_username(params[:username]) 
    Visit.track(@user, request.remote_ip) 
    end 

    def home 
    end 

end 

# application_controller.rb changes to devise to allow for other fields in forms. 
before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(:username, :first_name, :last_name, :email, :password, :password_confirmation) 
    end 
    devise_parameter_sanitizer.for(:account_update) do |u| 
     u.permit(:username, :first_name, :last_name, :bio, :email, :password, :password_confirmation, :current_password, :avatar, :cover_image) 
    end 
    end 

在頭我把它顯示在用戶個人資料圖像,並在配置我使用相同的代碼從頭。這裏是在這個鏈接圖片中的個人資料和標題圖片:http://4stor.com/ujp7F

但是,當它使用@user對象而不是current_user對象設計時,它會返回一個如下所示的URL:「/ avatars/thumb /missing.png「而不是獲取S3網址。但事情是,它存在......它顯示在標題中。

我在這裏非常愚蠢,並沒有線索可能會導致回形針抓住這個網址,而不是正確的網址。 StackOverflow上的其他答案沒有幫助,但類似的問題。謝謝! :)

+0

請分享Controller的相關代碼。將其添加到問題中。 –

+0

感謝您分享代碼。請看我的答案。 –

回答

1

如果您沒有看到使用@user.avatar.url(:thumb)的頭像,那麼這意味着在這種情況下由@user引用的用戶記錄沒有關聯的頭像。因此,missing.png

這裏是思想的一個食物,你可以檢查由current_user@user

def show 
    @user = User.find_by_username(params[:username]) 
    puts @user.inspect ## See which record @user is referring to, check if it has an avatar 
    Visit.track(@user, request.remote_ip) 
    end 

你也可以做puts current_user.inspect,看到了CURRENT_USER信息。

簡而言之,current_user@user不指向相同的記錄。

讓我知道你的inspect的調查結果。

+0

謝謝!這工作...我一定會嘗試下次遇到這樣的問題。我試圖查看的我的current_user和個人資料(我的個人資料)完全不同。我重置我的數據庫,現在一切都很好。非常感謝:) – g33kidd

+0

很高興幫助:) –

相關問題