Facebook の連絡先をすべてインポートする Rails アプリがあります。これには時間がかかります。インポートが後ろで行われている間、「お待ちください」ページを表示できるようにしたいと思います。
コントローラーの同じアクションに 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 gem を使用)、ホームページに直接移動します。
fb_graph 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
終わり
編集
以下に提案する解決策を追加しました。
アクション「待機」からの「連絡先をインポートする間待機」ビューは次のとおりです。
<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') %>
ありがとう!