Search code examples
ruby-on-railspaperclipasset-pipelinedevelopment-environmentproduction-environment

facing issue with image url in production mode


i have this type of code

module PhotosHelper 
  def photo_default_image_url(photo)
    if photo.image_url == "/assets/image.jpg"
        URI.join(root_url, "/assets/image.jpg")
    else
        photo.image_url(:thumb)
    end
  end
end

and image code is 

 <img src="<%= photo_default_image_url(@photo) %>">

its working fine for development mood.

i got url like this for image

http://localhost:3000/assets/image.jpg

but its not working in production mood

i got url like this for image

/image.jpg

please help me to solve this. thank you


Solution

  • Did you precomple your assets.As in production,the rails looks for assets in public folder and not in assets folder.i might be wrong but it can be because of asset pipeline.You can resolve this by manually adding the image in public folder and then running rake assets:precompile so that it is available in public folder. Morever,for the same stuff to handle default picture,you can use my code as well.

    ###in picture.rb
    ## belongs_to :user
    MISSING_PICTURE_URL = '/assets/picture_missing.png'
    
    ##in user.rb
    ##has_many :pictures
    def get_picture_url
      self.pictures.exists? ? self.pictures.sample.picture.expiring_url(:medium) : Picture::MISSING_PICTURE_URL
    
    end
    

    so i can easily use @user.get_picture_url and it works great. Also one more thing to note here,i am using use exists? and not present?