简体   繁体   中英

run controller before rendering html ruby on rails

In my rails app, HTML is rendered before controller completely runs. I want index method in controller to first run completely before rendering HTML. I was able to achieve this by adding <% sleep(x) %> at the beginning of the HTML page but i want to do it without using it. can anyone suggest a way to accomplish this?

Thanks

From the comment above you are using this index action:

def index 
  @data=Csvtest.all
  @length=@data.length-1 
  @resp=[] 
  threads = (0..@length-1).map do |i| #services are pinged concurrently 
    Thread.new(i) do |i| 
      @resp[i] = %x{curl -sL -w "%{http_code}" #{@data[i]['url']} -o /dev/null}.to_i #@response contains status code 
    end 
  end 
end

You are forgetting to say main thread to wait before all other threads terminates. You should call

threads.each { |t| t.join }

It tells, main thread wait until all of concurrent threads will pass to the end and than continue.

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