所以,目前我有一個問題,獲取個人資料頁面的頭像和個人資料封面圖片的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上的其他答案沒有幫助,但類似的問題。謝謝! :)
請分享Controller的相關代碼。將其添加到問題中。 –
感謝您分享代碼。請看我的答案。 –