好的,我有代码在引擎盖下使用 Cramp\Tramp => EventMachine。代码:
class RetrieveController < ApplicationController
on_start :retrieve_messages
#nonimportant stuff
def receive_messages
#other stuff
@current_user = false
User.where(User[:id].eq("request.env['rack.session']['user_id']")).all method(:retrieve_current_user)
if wait_for_current_user
EM.add_periodic_timer(1) {wait_for_current_user}
else
render @current_user
finish
end
end
def wait_for_current_user
if @current_user
render "current_user is set"
true
else
render "waiting for current_user"
false
end
end
def retrieve_current_user(users)
users.each do |user|
@current_user = user.name
end
end
end
我需要查询的结果才能继续在控制器操作中执行,但看起来执行在我获取数据之前已经完成。渲染文本为:
等待 current_user 假
我的宝石文件是:
source 'http://rubygems.org'
gem 'cramp', '~> 0.12'
gem 'tramp', '~> 0.2'
gem 'activesupport', '3.0.4'
gem 'rack', '~> 1.2.1'
gem 'eventmachine', '~> 0.12.10'
gem 'usher', '~> 0.8.3'
gem 'thin', '~> 1.2.7'
gem "bcrypt-ruby", :require => "bcrypt"