简体   繁体   中英

How to display flash object after sidekiq worker finishes-rails

I am trying to display reminder notification as a flash message in my rails app using sidekiq. I learnt how to set the flash message in the worker from this answer .

As mentioned in the last point of that answer, how to show the notification automatically, when an entry is made?

Also how to delete that entry once the flash message is closed?

I'm a beginner. Kindly help.

As requested, I'm adding my code attempt:

In my controller:

def show
  @user = User.find(params[:id])
  msg = $redis.get("messages:#{@user.id}")
  flash[:info] = msg if !msg.nil?
end

In my sidekiq worker:

def perform user, type
  @user = user`enter code here`
  message = "Message content"
  $redis.set("messages:#{user.id}", message)
end

What I want is to refresh user page when the redis key is set and also delete the pair when the close button of the flash is pressed.

Sidekiq is designed for asynchronous job processing. Once you've created a job, your controller is done and renders the page for the browser. Your job might be processed 1ms from now or 1 hour from now, you don't know. In other words, you can't do what you want. You should design your page flow to either account for this or don't create a background job for the work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM