AJAX calls the rails controller for the image without finding the correct file path

I have an image available in ruby ​​on

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

Images are saved using paperclip, and the user model does not have the url set:

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

There is a steering wheel template that should display the same image. Thus, I created a method get_imagein user_controller, but it does not return the correct URL for the missing image (it is used for users with images) and just shows the image with the broken image.

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);
            }

        });
    };

Result http://localhost:3000/profile/original/missing.png

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

Ruby works, the other does not. How to do this to bring up the correct path to the image? Thank!

+4

Source: https://habr.com/ru/post/1662162/


All Articles