set timeout on certain rails controller action

user3576036 Source

I have quiz app where there are multiple questions with options to select.

The controller actions are as follows

replies_controller.rb

#the questions appear on `new` page

 def new
    @quiz = Quiz.find(params[:quiz_id])
    @reply = @quiz.replies.build
    @quiz.questions.each {|question| @reply.answers.build(question: question)}
  end

#once user submits the answers it goes to create

  def create
    @quiz = Quiz.find(params[:quiz_id])
    @reply = @quiz.replies.build reply_params
    if @reply.save
      redirect_to @quiz, notice: "Thank you for taking the quiz"
    end    
  end

What I am trying to achieve is , after like 30 minutes it should automatically create. If creating empty params is a bad idea then at-least there should be way to show a message or redirect to root page after 30 minutes. How can I achieve this ?

ruby-on-rails

Answers

answered 3 months ago Ryan Clark #1

I think your best bet is going to be to use something like Sidekiq to delay the creation of a reply. You can create an empty one and send the id, along with the params to a worker and delay the rest of the work for a set period of time, like 30 minutes. You'll need a worker, something like a ReplyCreationWorker to call in the controller, then you can redirect with a message to check back in 30 minutes.

comments powered by Disqus