我有一个名为 Scanner 的模型,其目的是通过主动存储上传一个 xlsx 文件,然后在后台使用 sidekiq 和 roo gem 处理该文件以创建一个新的 txt 文件。该过程运行良好。我被卡住的部分是在读取 xlsx 文件的过程中,我想在显示视图中显示分析的行数,直到完成生成 txt 文件,这就是为什么我正在实施动作电缆但仍然无法正常工作的原因。这里的代码:
请任何帮助,在此先感谢。
/app/models/scanner.rb
class Scanner < ApplicationRecord
has_one_attached :target_file
has_many_attached :documents
after_create_commit do
ScannerBroadcastJob.perform_later(self)
end
end
/app/jobs/scanner_broadcast_job.rb
class ScannerBroadcastJob < ApplicationJob
queue_as :default
def perform(scanner)
ActionCable.server.broadcast('scanners_channel', {
analized_rows: render_analized_rows(scanner)
})
end
private
def render_analized_rows(scanner)
ApplicationController.renderer.render(partial: 'scanners/scanner',
locals: { scanner: scanner })
end
end
/app/channels/scanner_channel.rb
class ScannerChannel < ApplicationCable::Channel
def subscribed
stream_from "scanners_channel"
end
def unsubscribed
end
def create(data)
Scanner.create({ analized_rows: data['analized_rows'] })
end
end
/app/assets/javascripts/channels/scanner.coffee
App.scanner = App.cable.subscriptions.create "ScannerChannel",
connected: ->
disconnected: ->
received: (data) ->
$("#analized_rows").append(data["analized_rows"])
create: (analized_rows) ->
@perform 'create', analized_rows: analized_rows
/app/views/scanners/_scanner.html.erb
<div class="scanner" id='analized_rows'>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<table class="table">
<tr>
<th>Filename</th>
<td class="text-center"><%= scanner.filename %></td>
</tr>
<tr>
<th>Status</th>
<td class="text-center"><%= scanner.status %></td>
</tr>
<tr>
<th>Analized Rows</th>
<td class="text-center"><%= scanner.analized_rows %></td>
</tr>
<tr>
<th>Target File</th>
<td class="text-center"><%= link_to
scanner.target_file.blob.filename, url_for(scanner.target_file)
%> </td>
</tr>
</table>
</div>
</div>