Rails "Wait"

I have a rails app that will import all your Facebook contacts. It takes some time. I would like to show a "Wait, wait" page while the import is going backwards.

It seems that I cannot put render and redirect_to in the same action in the controller. How can i do this?

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'

      

If this is the first user on the site, I want to display the "Wait, Wait" page, start importing and after redirecting it to the root path, where a lot of processing is taking place with this data.

If this is not the first time, then install a contact update in the background (using the delayed_jobs gem) and go straight to the home page

I am using fb_graph gem to import contacts. Here's the method

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

      

end

Edit

I added the below suggested solution, it works!

Here is my wait while we import the contact view from the "wait" action

<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') %>

      

Thank!

+3


source to share


1 answer


One request gets one response - you cannot display content and redirect.



If I were you, I would always be doing a lengthy process in lazy work - pinning passenger / unicorn instances is never a great idea. Display the Wait Wait page, which is periodically refreshed to check if the job has been deferred (if you pinned the id of the pending job, you can check to see if it is all in db. When te completion is removed). When the task is completed, redirect to the results page. You can also do the periodic check bit via ajax.

+1


source







All Articles