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!