如何在演示者之间共享演示者方法?

时间:2015-07-03 16:48:54

标签: ruby-on-rails

我试图在Rails 4应用程序中从另一位演示者调用presenter方法。视图中没有任何内容呈现。设置它的正确方法是什么?

例如,如果我想在活动Feed中显示用户演示者,请使用Ryan Bates示例作为基础

用户主持人:http://railscasts.com/episodes/287-presenters-from-scratch

活动Feed演示者:http://railscasts.com/episodes/407-activity-feed-from-scratch

我正在尝试做类似

的事情
class ActivityPresenter < SimpleDelegator
....
  def render_activity
    div_for activity do
      present action.user do |user_presenter|
        user_presenter.linked_name
      end
    end
  end
...
end

class UserPresenter < BasePresenter
...
  def linked_name
    site_link(user.full_name.present? ? user.full_name : user.username)
  end
...
end

用户未被&#34;呈现&#34;。我究竟做错了什么?

使用辅助方法初始化演示者

def present(object, klass = nil)
  klass ||= "#{object.class}Presenter".constantize
  presenter = klass.new(object, self)
  yield presenter if block_given?
  presenter
end

虽然也可以在控制器中访问:

def present(object, klass = nil)
  klass ||= "#{object.class}Presenter".constantize
  klass.new(object, view_context)
end

在视图中启用渲染,如下所示:

<% present @user do |user_presenter| %>
    <%= user_presenter.linked_name %>
<% end %>

0 个答案:

没有答案