我是Rails的新手,我一直在尝试使用Active Job和Action Cable以及Presenter和我在所有地方进行搜索,但却无法弄明白。
所以我的问题是,有没有办法在局部使用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中呈现,因为我假设的会话主持人。 所以我的问题是,无论如何使用演示者在控制器外部进行渲染?
如果您需要有关我代码其他部分的任何信息,请与我们联系
谢谢!
答案 0 :(得分:1)
我使用以下代码足以从ActiveJob运行它:
Illuminate
摘自https://makandracards.com/makandra/17751-render-a-view-from-a-model-in-rails
答案 1 :(得分:0)
尝试使用
ConversationsController.render - &gt;的 ApplicationController.renderer.render 强>
这对我有所帮助。 def render_conversation(conversation)
renderer=ApplicationController.renderer.new
renderer.render(partial: 'conversations/conversation',
locals: {conversation: conversation, var: 'not'})
end
答案 2 :(得分:0)
以下在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,以获取更多信息。