微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails-3 – 如何从演示者层渲染轨道?

好吧,我现在也使用rails 3.2.1进入这种情况
以下是app / presenters / form_presenter.rb中的演示者

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

从我所说的观点来看,

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

但它有以下错误

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:
...

RAILS-3.1 render method for ActionView::Base有这个类似的问题,但它没有帮助.

如何从演示者层渲染此部分?

解决方法

好吧,我只是通过使用前置过滤器抓取视图上下文来做到这一点.我的参考是: 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐