Pregunta

I have a model called Recipe which has 2 images that use carrierwave, so in this model i have this to setup carrierwave

mount_uploader :author_photo, AuthorUploader
mount_uploader :photo, PhotoUploader

I have also added multiple version to my images such as thumb, small, medium, large

The problem is. say i have 2 images

Chocolate_Cake.jpg as the photo
My_Photo.jpg as author_photo

When i go into console and load up my recipe and to recipe.to_json, I get both my images back from carrierwave but they are both showing the photo for the recipe, not the author photo.

     "recipe": [
    {
        "author_photo": {
            "url": "/uploads/recipe/photo/8/Chocolate_Cake.jpg",
            "thumb": {
                "url": "/uploads/recipe/photo/8/thumb_Chocolate_Cake.jpg"
            },
            "small": {
                "url": "/uploads/recipe/photo/8/small_Chocolate_Cake.jpg"
            },
            "medium": {
                "url": "/uploads/recipe/photo/8/medium_Chocolate_Cake.jpg"
            },
            "large": {
                "url": "/uploads/recipe/photo/8/large_Chocolate_Cake.jpg"
            }
        },
        "id": 8,
        "photo": {
            "url": "/uploads/recipe/photo/8/Chocolate_Cake.jpg",
            "thumb": {
                "url": "/uploads/recipe/photo/8/thumb_Chocolate_Cake.jpg"
            },
            "small": {
                "url": "/uploads/recipe/photo/8/small_Chocolate_Cake.jpg"
            },
            "medium": {
                "url": "/uploads/recipe/photo/8/medium_Chocolate_Cake.jpg"
            },
            "large": {
                "url": "/uploads/recipe/photo/8/large_Chocolate_Cake.jpg"
            }
        },

So for some reason my json response isnt showing my uploaders properly.

If i was to type this in console,

 recipe.photo
 recipe.author_photo

They come up with different image urls

¿Fue útil?

Solución

After some research and a help from a friend, i found that i could override the as_json method for the recipe model to fix the response i was getting.

   def as_json(options = {})
     super.merge('photo' => photo.as_json[:photo], 'author_photo' => author_photo.as_json[:author_photo])
   end

This solution worked.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top