سؤال

So, currently I am having an issue with getting the URL for an avatar and profile cover image for a profile page.

The interesting thing is that it will show if I use:

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

In the header I have it displaying the user profile image and on the profile I use the same code as from the header. Here is the profile and the header with the profile image in the picture at this link: http://4stor.com/ujp7F

But when it's using the @user object instead of the current_user object from devise it returns a URL like this: "/avatars/thumb/missing.png" instead of getting the S3 url. But the thing is, it exists... it's shown in the header.

I am extremely dumbfound here and have no clue what could be causing paperclip to grab this url instead of the correct url. Other answers here on StackOverflow have not been helpful, but similar issues. Thanks! :)

هل كانت مفيدة؟

المحلول

If you don't see an avatar using @user.avatar.url(:thumb) then that means the user record referred by @user in this case does not have an associated avatar. Hence, the missing.png.

Here is a food for thought, you can inspect the current_user and @user by:

  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

You can also do puts current_user.inspect and see the current_user info.

In nutshell, current_user and @user are not pointing to the same record.

Let me know your inspect findings.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top