4

Well, I'm into this situation as well now using rails 3.2.1 Following is the presenter in app/presenters/form_presenter.rb

class FormPresenter
  def render_form
    ActionView::Base.new.render partial: "passions/add_form"
  end
end

From the view I'm calling,

...
= AddFormPresenter.new.render_form
...

But it blows with the following error:

13:14:12 ActionView::MissingTemplate - Missing partial passions/passion_concept_add_form with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :slim, :coffee, :rabl]}. Searched in:
...

There is this similar question at RAILS-3.1 render method for ActionView::Base but its not helpful.

How to render this partial from the presenter layer?

4

2 回答 2

2

好吧,我只是通过使用前置过滤器获取视图上下文来做到这一点。我的参考是这样的:https ://github.com/amatsuda/active_decorator/blob/master/lib/active_decorator/view_context.rb

所以像:

class FormPresenter
  def render_form
    FromPresenter.view_context.render partial: "passions/add_form"
  end

  class << self
    def view_context
      Thread.current[:view_context]
    end

    def view_context=(view_context)
      Thread.current[:view_context] = view_context
    end
  end

  module Controller
    extend ActiveSupport::Concern

    included do
      before_filter do |controller|
        FormPresenter.view_context = controller.view_context
      end
    end
  end
end

并在 application_controller.rb

class ApplicationController < ActionController::Base
...
   include FormPresenter::Controller
...
end
于 2012-12-30T15:54:52.713 回答
1

这不是典型的演示者模式。Presenter 用于集中简化视图渲染任务所需的复杂数据和逻辑。在这里,您正在演示者内部进行渲染。这真的是你想要的吗?

说答案是肯定的。然后仅仅创建一个新ActionView::Base的就是自找麻烦,因为初始化它并不简单,如此处所示。类或其他类型的嵌套发生了一些奇怪的事情。passion_concept_错误消息中的前缀来自哪里?您似乎没有告诉我们我们需要的关于您的应用的所有信息。

通过明确地告诉演示者它在哪里呈现,您可能会感到高兴:

class FormPresenter

  def self.render_form(view)
    view.render partial: "passions/add_form"
  end

end

然后在视图中:

= FormPresenter.render_form(self)

(这里的解释也不清楚。什么是AddFormPresenter?)我目前没有可以尝试这个的机器,但它应该比你所拥有的更易于调试。

于 2012-12-28T02:50:37.647 回答