title: How to use delayed_job to delay a controller action?
body: <p>How can I implement delayed_job gem to this action/actions? It gets plist from an API and inserts those values to database. I just want it insert datas as a background process...</p>
  
  <pre><code>def get_videos_from_outer(page=params[:page], kid=params[:kid], totalCount="")
  
          @videos = Video.where(program_id: params[:p_id])
          @program = Program.find(params[:p_id])
  
          @fetch = Typhoeus::Request.post("URL", :params =&gt; {:commandtype=&gt;"getprogramepisodes", :feedtype=&gt;"plist",:id=&gt;kid.to_i, :page=&gt;page.to_i})
          @plist = Plist::parse_xml(@fetch.body.force_encoding 'utf-8')
  
  
  
            @totalCount = @plist.second.first['totalCount']
  
            if !totalCount.blank?
              @totalCount = totalCount
            end
          import_outer_videos(@plist, kid, page.to_i, @totalCount.to_i)
  
    end
  </code></pre>
  
  <p>And the <code>import_outer_videos</code> method.</p>
  
  <pre><code>private
  
  def import_outer_videos(plist, kid, page, totalCount)
  
           @totalCount = totalCount
  
          plist.second.each_with_index do |t, i| 
          # First page has odd data and here we're getting rid off them
                  if page.to_i==1
                     if i &gt; 0
                     @new = Video.create(:thumb_path=&gt;t['tnPath'], :vid=&gt;t['id'], :title=&gt;t['title'], :type=&gt;t['type'], :kid=&gt;kid, :program_id=&gt;@program.id)
  
                      end
                  else
                    @new = Video.create(:thumb_path=&gt;t['tnPath'], :vid=&gt;t['id'], :title=&gt;t['title'], :type=&gt;t['type'], :kid=&gt;kid, :program_id=&gt;@program.id)       
  
                  end
  
          end
  
          if page.to_i &lt; (@totalCount.to_i/20) + 1
          page = page.to_i + 1 
  
          get_videos_from_outer(page.to_i, kid.to_i, @totalCount)
          else
            redirect_to :action=&gt;"index", :p_id=&gt;params[:p_id]
          end
  
            if @new.errors.blank?
            flash[:notice]="#{@totalCount} videos has been transfered."
            else
              flash[:notice]="No new video."
            end
  end
  </code></pre>