我有一个导入所有Facebook联系人的Rails应用程序。这需要一些时间。我希望能够显示“请稍候”页面,而导入仍在后面进行。Rails“please wait”页面
看来,我不能把控制器中的render和redirect_to放在同一个动作上。我怎样才能做到这一点?
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宝石),直接进入主页
我使用fb_graph宝石导入联系人。这里的方法
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
末
编辑
我增加了以下解决方案建议,它的工作原理!
这里的是我“请稍等,我们导入联系人”视图从动作“等待”
<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') %>
谢谢!
你有没有想过使用ajax?也许这会导致更容易和更流畅的解决方案。 – alex 2012-04-07 10:47:48
AJAX是您的解决方案。如果您使用的是jQuery,请查看http://api.jquery.com/jQuery.get/。 jQuery中的AJAX提供了一个回调函数,当请求返回时将执行回调函数(在您的情况下导入联系人)。请求返回后,你可以查看你想要的任何消息(在你的情况“成功登录”) – mohamagdy 2012-04-07 11:02:09
我确实想过使用Ajax。但我使用[fb_graph gem](https://github.com/nov/fb_graph)导入,并且我不想闯入宝石代码......请参阅上面编辑的答案 – wachichornia 2012-04-07 11:11:04