【问题标题】:Rails 5 Render view/partial with Active Job with presenterRails 5 渲染视图/部分与演示者的活动作业
【发布时间】:2017-08-11 00:15:12
【问题描述】:

我是 Rails 新手,我一直在尝试 Active Job 和 Action Cable 以及 Presenter,我到处搜索,但无法弄清楚。

所以我的问题是,有没有办法在部分中使用演示者并将其呈现在控制器之外(例如在 ActiveJob 中)?

以下是我想要实现的目标:

我有一个消息系统(更像是电子邮件),带有对话控制器和消息控制器。我想要实现的是当有人开始新对话时,如果接收者在对话索引页面上,新对话会自动附加到他们的索引视图中。(就像在电子邮件中一样)使用 ActionCable

所以,我的对话.rb

    class Conversation < ApplicationRecord
    has_many :messages, dependent: :destroy
    after_create_commit { ConversationBroadcastJob.perform_later(self) }

conversation_broadcast_job.rb

    class ConversationBroadcastJob < ApplicationJob
     queue_as :default

       def perform(conversation)
        ActionCable.server.broadcast "conversations:#{conversation.receiver_id}",
                              {convo: render_conversation(conversation)}
      end

      private

      def render_conversation(conversation)
        ConversationsController.render(partial: 'conversations/conversation',
                     locals: {conversation: conversation, var: 'not'})
      end

    end

还有我的 _conversation.html.erb,我按照 RailCast 的这一集使用了 Presenter

    <% present conversation do |conversation_presenter| %>

    <div class="conversation <%=conversation_presenter.css(var)%>"
         id="conversation_<%= conversation.id %>"
         data-sender-id='<%= conversation_presenter.sender_id%>'>

      <%=conversation_presenter.avatar%>

      <div class="conversation-counterpart-name conversation-info truncate">
        <%=conversation_presenter.name(var) %>
      </div>


      <div class='conversation-subject conversation-info truncate'>
        <%=link_to conversation_messages_path(conversation),
                                      class:'link-gray truncate' do |n|%>
          <%=conversation_presenter.subject %>
           <div class='last-message truncate'>
            <%=conversation_presenter.last_message%>
          </div>
        <% end %>
      </div>

      <div class="conversation-date conversation-info">
        <%=conversation_presenter.last_date%>
      </div>

    </div>

    <% end %>

当前方法在ApplicationHelper中定义

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

conversation_presenter.rb

    class ConversationPresenter < BasePresenter

      presents :conversation
      delegate :subject, to: :conversation


      def name(var)
        if conversation.sender_id == var.id
          conversation.receiver.name
        else
          conversation.sender.name
        end
      end

      def last_date
        if conversation.last_msg.nil?
          conversation.render_date
        else
          conversation.last_msg.render_date
        end
      end

      def last_message
        conversation.last_msg.content unless conversation.last_msg.nil?
      end

      def avatar
        h.image_tag('avatar.svg', size: 30, class:'conversation-        counterpart-avatar')
      end

      def css(var)
        "unread" unless
        conversation.read || (!conversation.last_msg.nil? &&     conversation.last_msg.wrote_by(var))
      end

      def sender_id
        if conversation.last_msg.nil?
          conversation.sender_id
        else
          conversation.last_msg.author_id
        end
      end

    end

问题从这里开始,由于我假设的对话演示者,部分不会在 ActiveJob 中呈现。 所以我的问题是无论如何都可以在控制器外部使用演示者进行渲染?

如果您需要有关我的代码其他部分的任何信息,请告诉我

谢谢!

【问题讨论】:

    标签: ruby-on-rails-5 actioncable rails-activejob presenter


    【解决方案1】:

    以下适用于 Rails 5:

    ApplicationController.render(
      :template => 'users/index',
      :layout => 'my_layout',
      :assigns => { users: @users }
    )
    

    示例:这可以在工作文件夹或您希望的任何其他位置。

    我在工作中使用过 (jobs/invoice_job.rb)

    invoice_obj = Invoice.first
    # Process the pdf_attachement in any way you want
    pdf_attachement = WickedPdf.new.pdf_from_string(
          ApplicationController.render(template: 'invoices/invoice_pdf.html.erb', layout: 'layouts/pdf.html.haml', assigns: { invoice: invoice })
        )
    

    在如下视图中使用它:(views/invoices/invoice_pdf.html.erb)

    <%
    @invoice = assigns[:invoice]
    %>
    <%= render 'invoices/invoice_pdf_template' %> <!-- generate your pdf html -->
    

    pdf 布局:(layouts/pdf.html.haml)

    !!!
    %html
      %head
        %title RailsWickedPdf
        = wicked_pdf_stylesheet_link_tag "application", :media => "all"
        = wicked_pdf_stylesheet_link_tag "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css", media: "all"
        = wicked_pdf_javascript_include_tag "application"
        = csrf_meta_tags
      %body
        = yield
    

    更多信息请参考here

    【讨论】:

      【解决方案2】:

      我使用以下代码足以从 ActiveJob 运行它:

      ApplicationController.new.render_to_string(
        :template => 'users/index',
        :layout => 'my_layout',
        :locals => { :@users => @users }
      )
      

      摘自https://makandracards.com/makandra/17751-render-a-view-from-a-model-in-rails

      【讨论】:

        【解决方案3】:

        尝试使用

        ConversationsController.render -> ApplicationController.renderer.render

        这对我有一次帮助。

          def render_conversation(conversation)
               renderer=ApplicationController.renderer.new
               renderer.render(partial: 'conversations/conversation',
                             locals: {conversation: conversation, var: 'not'})
        
          end
        

        【讨论】:

        • 您能提供更多信息吗?我试过了,但没有用 ConversationsController.render -> ApplicationController.renderer.render {(partial: 'conversations/conversation', locals: {conversation: conversation, var: 'not'})}
        • @Rosalie 我在尝试从作业中渲染时遇到了同样的问题。通常作业继承自 ApplicationJob(ActiveJob::Base)。但 render 是 ApplicationController 方法。因此,要从作业中渲染,您需要将 Conversationscontroller 更改为 aplicationcontroller.renderer。我在回答中举了一个例子。
        • @Rosalie 试着放一些日志。看到是问题
        猜你喜欢
        • 2015-05-30
        • 2013-10-08
        • 1970-01-01
        • 2020-01-29
        • 2018-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-24
        相关资源
        最近更新 更多