1

我创建了一个像这样的用户助手:

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

但是,我收到一个错误:

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

目前我有以下内容:

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
4

2 回答 2

1

asset_path问题可能在于 Sprockets ( ) 中定义的事实,Sprockets::Helpers::RailsHelper而不是您包含的帮助程序。

我宁愿建议使用在您的情况下定义的image_pathhelper (或其别名),而不是包含另一个助手,并且在您的情况下应该开箱即用。path_to_imageAssetTagHelper

于 2012-09-10T14:48:30.137 回答
1

尝试添加这个:

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

它将解决您的问题。

于 2013-03-08T18:37:35.740 回答