Volodymyr
Volodymyr

Reputation: 1186

asset_path in custom class

I've created a user helper like so:

module UserHelper

  def user_photo user, size = 30
    image_tag user_avatar_url(user, size), :height => size, :width => size, :title => user_name(user), :alt => '' if user
  end

  alias :user_avatar :user_photo

  def user_avatar_url user, size = 30
    user.image ? user.image.thumb("#{size}x#{size}#").url : asset_path('icons/unknown-user-icon.png')
  end
end

However, I get an error:

undefined method `asset_path' for #<UserProfileHtmlTemplater:0x0000000680aad8>

Currently I have following:

class UserProfileHtmlTemplater < CompanyTextTemplater

  include Rails.application.routes.url_helpers
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::OutputSafetyHelper 
  include ActionView::Helpers::AssetTagHelper 
  include ActionView::Helpers::TagHelper 
  include UserHelper

....
  def prepare_locals_for(user)
    Hash.new.tap do |locals|
      locals[:first_name] = user.first_name
      locals[:last_name] = user.last_name
      locals[:email] = user.email

      locals[:photo]   = raw user_photo(user, '200x200>')
      user.profile.visible_fields.each do |field|
        locals[field.label] = field.value
    end
  end
end

Upvotes: 1

Views: 2152

Answers (2)

mrded
mrded

Reputation: 5290

Try to add this:

include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper

It will fix your problem.

Upvotes: 1

Radek Paviensky
Radek Paviensky

Reputation: 8486

The problem is probably in the fact that asset_path is defined in Sprockets (Sprockets::Helpers::RailsHelper) and it's not in your included helpers.

Instead of including yet another helper I'd rather recommend to use image_path helper (or its alias path_to_image) that is defined in AssetTagHelper and in your case should work out of the box.

Upvotes: 1

Related Questions