2016-06-20 4 views
0

カスタムPDFが作成され、Paperclip経由でモデルに添付されている間に簡単なメッセージを表示しようとしています。完了したらプレビューを表示します。データベース内の処理状態を経由してSidekiqジョブを完了するための作業

私が見つけることができる最も簡単な解決策は、this questionのMike Perhamの応答です。 "データベースを使用して各写真の状態を保存し、Sidekiqに状態を更新させる。"

私はまだJavascript、JQuery、Rails &の良いSOの質問を書く方法を学んでいることに気づくはずです。それにもかかわらず、これまで私がフランケンシュタインに行ってきたことがあります。

私がここの正しい方向に取り組んでいるかどうか教えてください。

# generate_insert.rb 
class GenerateInsert 
    include Sidekiq::Worker 
    def perform(customization_id) 
    customization = Customization.find(customization_id) 
    customization.update_attribute(:processing, true) 
    # code to perform, generate PDF via Prawn and attach via Paperclip 
    customization.update_attribute(:processing, false) 
    customization.save! 
    end 
end 

# customizations/show.html.erb 
<div id='message'>First Message</div> 

# messages.js 
var i = 1; 
var sampleMessages = [ "First Message", "Second Message", "Third Message" ]; 
    setInterval(function() { 
     var newText = sampleMessages[i++ % sampleMessages.length]; 
     $("#message").fadeOut(500, function() { 
      $(this).text(newText).fadeIn(500); 
     }); 
    }, 1 * 3000); 
}); 

# show.js.erb 
$("#message").replaceWith("<%= j render 'customization' %>"); 

# poller.js 
CustomizationPoller = 
poll: -> 
    setTimeout @request, 5000 

request: ->, 
    $.get($('???').data('url')) 

jQuery -> 
if $('#???').length > 0 // customization processing? 
    CustomizationPoller.poll() 

答えて

1

あなたはあなたが正しい道にいるようです。ユーザーがより堅牢なソリューションを要求するまで簡単に保つことは、IMHOは良いアイデアです。

このようなインタラクションを増やす(ユーザーが何かをして更新を待つ)場合は、ウェブソケットの使用を検討するか、https://rethinkdb.comなどのツールを使用することもできます。もちろん、それはあなたが持っていることを予定しているこのような多くの相互作用に依存します。

+0

ありがとうございました。 – rclark

関連する問題