【发布时间】:2013-12-06 10:08:21
【问题描述】:
我正在尝试将一些逻辑从我的观点中移出,并且我正在使用演示者来做到这一点。受影响的代码如下。
演讲者:
class ApplicationPresenter < ActionView::Base
extend Forwardable
include Rails.application.routes.url_helpers
end
class ProfilesPresenter < ApplicationPresenter
def_delegators :@profile, :id
def initialize(profile, user)
@profile = profile
@user = user
end
def container(&block)
path = profile_insights_path(id)
link_to capture(&block), path
end
end
视图部分:
(这里的profile是一个presenter对象)
<%= content_tag(:li) do %>
<%= profile.container do %>
Some Stuff
<% end %>
<% end %>
我想基本上像助手一样使用 Presenter 方法,因此配置文件对它的行为方式有一个“意见”。目前,container 方法中没有任何逻辑,但会有。
我遇到的唯一问题是输出显示两次:
<li>
Some Stuff
<a href="/profiles/11725854/insights">Some Stuff</a>
</li>
如何防止方块在第一次触发?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 actionview