0

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
Antarr Byrd
  • 20,852
  • 26
  • 85
  • 154
  • I think you're running into a naming collision and I believe ruby will execute the first method matched which is image_url on your model and not the helper method, you should namespace your helper methods so you could do something like: `Model::Helper.image_url(path)` – Zach Tuttle Sep 05 '18 at 17:15

3 Answers3

5

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)

arieljuod
  • 13,456
  • 2
  • 19
  • 31
0

I'll agree with arieljuod that this is a view concern. However, in case it's really needed for a dirty implementation, it is possible to do Object.new.extend(ActionView::Helpers::AssetUrlHelper).image_url('foo')

Matthieu Libeer
  • 1,938
  • 1
  • 9
  • 13
0

Yea, it's very right, you can't call on that scope! This isn't a method for a model. This is an ActionView helper method. https://apidock.com/rails/ActionView/Helpers/AssetUrlHelper/image_url

For what I'm seeing this isn't the right place to have this logic. Consider putting this in a decorator to abstract this non-business logic.

VinyLimaZ
  • 11
  • 4