4

I have an image that is reachable in ruby by

<%= image_tag(@user.avatar.url(:thumb)) %>

Images are stored using paperclip, and there is a default missing url set in the users model:

has_attached_file :avatar, :styles => { :medium => "200x200#", :thumb => "100x100#" }, :default_url => ":style/missing.png"

There is a handlebars template that needs to render this same image. So, I created a get_image method in the users_controller, but it does not return the correct url for the missing image (it does for users with images), and just shows a broken image picture.

def get_image
    user_id = params[:userId]
    user = User.find(user_id)
    @user_image = user.avatar

    render json: @user_image, :status => 200 
end

and calling it in application.js:

function getUserImage(userId)   {
        return $.ajax({ 
            type: "GET",
            url: '/get_image',
            data: {
                userId: userId
            },
            success: function(result)   {   
                console.log(result)
                userImage = result;
                return userImage;
            },
            error: function(err)    {
                console.log(err);
            }

        });
    };

The result is http://localhost:3000/profile/original/missing.png

The ruby image_tag returns: http://localhost:3000/assets/medium/missing-e38aa1831b278c511eff9812efc6fda028d46b3b94f71cc88c3e0ba0e99ff19e.png

The ruby one works, the other does not. How to do I get it to call the correct image path? Thanks!

gwalshington
  • 1,418
  • 2
  • 30
  • 60
  • What is the output of the json from `get_image` (ie your `console.log(result)`) because I think you'd want either `result.url` or to `render json: @user_image.url, status: 200` – Anthony Nov 28 '16 at 16:27
  • @Anthony `original/missing.png`. And the next line down is a 404 not found for http://localhost:3000/profile/original/missing.png – gwalshington Nov 28 '16 at 16:43
  • @Anthony when I change to `render json: @user_image.url, status: 200` the console returns: Object {readyState: 4, responseText: "original/missing.png", status: 200, statusText: "OK"}` and won't execute the handlebars template at all – gwalshington Nov 28 '16 at 16:49
  • See this: http://stackoverflow.com/questions/9646549/default-url-in-paperclip-broke-with-asset-pipeline-upgrade – Anthony Nov 28 '16 at 17:22
  • 1
    It's certainly a asset pipeline issue I'm just not sure what paperclip recommends – Anthony Nov 28 '16 at 17:24

0 Answers0