Rails 5使用Active Job呈现视图/部分主持人
问题描述:
我是Rails的新手,我一直在尝试使用Active Job和Action Cable以及Presenter,并且我搜索遍布整个地方但无法计算出出来。Rails 5使用Active Job呈现视图/部分主持人
所以我的问题是,有任何方式来使用部分呈现器,并呈现在控制器之外(例如在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
<% 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,因为谈话主持人我假设。 所以我的问题是,有无论如何使用主持人与外部控制器渲染?
请让我知道如果您需要了解我的代码的另一部分
感谢任何信息!
答
尝试使用
ConversationsController.render - >ApplicationController.renderer.render
这是帮我一次。
def render_conversation(conversation)
renderer=ApplicationController.renderer.new
renderer.render(partial: 'conversations/conversation',
locals: {conversation: conversation, var: 'not'})
end
答
我用下面的代码足以从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
提取你能否提供更多的信息?我尝试过,但它没有工作ConversationsController.render - > ApplicationController.renderer.render {(partial:'conversations/conversation', locals:{conversation:conversation,var:'not'})} – Rosalie
@Rosalie我也有同样的尝试从作业渲染时出现问题。通常作业继承自ApplicationJob(ActiveJob :: Base)。但是render是ApplicationController方法。所以要从作业渲染,您需要将Conversationscontroller更改为aplicationcontroller.renderer。 我举了一个答案的例子。 –
@Rosalie试着放一些日志。看到的是问题 –