Ruby on rails 铁路";请稍等;页
我有一个rails应用程序,可以导入你所有的Facebook联系人。这需要一些时间。我想能够显示一个“请等待”页面,而进口一直发生在后面 似乎我无法将render和redirect_设置为控制器中的同一操作。我该怎么做Ruby on rails 铁路";请稍等;页,ruby-on-rails,redirect,rendering,pleasewait,Ruby On Rails,Redirect,Rendering,Pleasewait,我有一个rails应用程序,可以导入你所有的Facebook联系人。这需要一些时间。我想能够显示一个“请等待”页面,而进口一直发生在后面 似乎我无法将render和redirect_设置为控制器中的同一操作。我该怎么做 if @not_first_time Authentication.delay.update_contact_list(current_user) else render 'some page telling the user to wait' Authe
if @not_first_time
Authentication.delay.update_contact_list(current_user)
else
render 'some page telling the user to wait'
Authentication.import_contact_list(current_user)
end
redirect_to :root_path, :notice => 'Succesfully logged in'
如果是用户第一次进入站点,我想呈现一个“请等待页面”,开始导入,一旦导入完成,就重定向到根路径,在那里对这些数据进行大量处理
如果不是第一次,那么将联系人更新放在后台(使用delayed_jobs gem)并直接转到主页
我正在使用fb_图形gem导入联系人。方法是这样的
def self.import_contact_list(user)
user.facebook.friends.each do |contact|
contact_hash = { 'provider' => 'facebook', 'uid' => contact.identifier, 'name' => contact.name, 'image' => contact.picture(size='large') }
unless new_contact = Authentication.find_from_hash(contact_hash)
##create the new contact
new_contact = Authentication.create_contact_from_hash(contact_hash)
end
unless relationship = Relationship.find_from_hash(user, new_contact)
#create the relationship if it is inexistent
relationship = Relationship.create_from_hash(user, new_contact)
end
end
结束
编辑
我在下面添加了建议的解决方案,效果很好
这是我在“等待”操作中的“导入联系人时等待”视图
jQuery(文档).ready(函数(){
$.get(“/import\u contacts”,函数(数据){
window.location.replace(“/”)
});
});
正在导入您的联系人,请稍候
谢谢 单个请求接收单个响应-您无法呈现内容和重定向
如果我是你,我总是会在延迟的工作中完成漫长的过程-捆绑乘客/独角兽实例从来都不是一个好主意。呈现定期刷新的“请等待页面”,以检查延迟作业是否已完成(如果您隐藏延迟作业的id,则可以测试它是否仍在数据库中。当te作业完成时,它将被删除)。作业完成后,重定向到结果页面。您还可以通过ajax进行定期检查。您考虑过使用ajax吗?也许这会导致更简单、更流畅的解决方案。AJAX是您的解决方案。如果您使用的是jQuery,请查看一下这个。ajaxget-in-jQuery提供了一个回调函数,当请求返回时(在您的例子中,联系人被导入)将执行该函数。在请求返回后,您可以查看您想要的任何消息(在您的案例中是“成功登录”),我确实想到了使用Ajax。但是我正在使用导入,我不想破坏gems代码。。。请参见上面编辑的答案构建一个页面,在该页面中呈现“某个页面告诉用户等待”,对控制器操作执行AJAX调用,该操作将从facebook导入联系人,并在导入完成后在此操作中进行呈现,您应该会没事!当收到AJAX响应时,用服务器的结果替换等待的消息!好的,谢谢!成功了!这是我最初的意图,但事实证明,延迟的_作业在服务器繁忙时从不开始处理数据,而且服务器总是繁忙的。因此,实际执行(测试)作业需要几个小时(如果不是永远不会的话)。有没有办法改变这种行为?这听起来不像是我见过的延迟作业做的事情——它不知道应用程序实例在做什么
<script>
jQuery(document).ready(function() {
$.get( "/import_contacts", function(data) {
window.location.replace("/")
});
});
</script>
<% title 'importing your contacts' %>
<h1>Please wait while we import your contacts</h1>
<%= image_tag('images/saving.gif') %>