Search code examples
ruby-on-railsrubyhelpermethods

Using image_url helper in model


I'm trying to use the image_url helper in my model. I also have an image_url property on the model(can't be changed). When I call image_url, the helper method it appears to be calling the method on the model. I get the error wrong number of arguments (given 1, expected 0)

 def public_image
    if self.avatar && photo_public?
      self.avatar.remote_url
    else
      image_url '/client/src/assets/images/icons/anon-small.svg'
    end
  end

Solution

  • image_url is a view helper, it should not be used inside the model, you should move that logic to a helper for the view

    #application_helper.rb
    def public_image(user)
      if user.avatar && user.photo_public?
        user.avatar.remote_url
      else
        image_url '/client/src/assets/images/icons/anon-small.svg'
      end
    end
    

    In your view change user.public_image to public_image(user)