2

Sorry for the question formulation, didn't really know how to explain my problem simply.

I created a plugin for the Redmine platform where the user can synchronise his Dropbox account with the documents tab on Redmine, everything is working fine but I have a little problem.

When the user click on the synchronise link, I would like to display a little message in my view to notify him that the work has started. Moreover, it would be great if a little popup shows up to let the user choose if he really wants to do the synchronisation because it might take a while.

I tried a few things:

First, I wrote my link_to like this:

<%= link_to 'Synchronise', {:controller => "projectusers", :action => "dropbox_sync", :id => p, :project_id => @project.identifier}, :class => "icon icon-reload", :confirm => "Are you sure you want to .. bla bla" %>

This is great but I do not know how to display a message in my view directly after the user accepts the synchronisation, is there a way to do that?

Second, I used some Javascript. I wrote a little script like this at the beginning of my view:

<script>
function displayMessage()
{
  alert("Sync is about to start blabla);
  $('#syncStarted').show(); 
}
</script>

Then a little hidden paragraph:

<p id="syncStarted", style="display:none"><strong><font color="green">Synchronisation has started, please wait...</font></strong></p>

And I call my function like this:

<%= link_to 'Synchronise', {:controller => "projectusers", :action => "dropbox_sync", :id => p, :project_id => @project.identifier}, :class => "icon icon-reload", :onclick => "displayMessage()" %>

I know this is not a really good Ruby/Rails way to implement JS and that my html is pretty obsolete (CSS would be much better), but it's working pretty well. The only problem with this is that the user has no choice, if he clicks on the link, the sync will start even if he closes the popup by clicking the cross.

Maybe could you help me to find the better way to do that, I think the use of the :confirm parameter is better but I could not find how to add a little notification on my page after the user confirms the action.

I'm working with Rails v3.2.16.

Thanks

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
Shredator
  • 940
  • 3
  • 17
  • 32

2 Answers2

1

Are you redirecting to another view after the sync? Then simply use the built-in flashes. On the redirect do:

 redirect_to after_sync_url, notice: "Sync has started."

And in the view after place something like this:

 <% flash.each do |name, msg| -%>
  <%= content_tag :div, msg, class: name %>
 <% end -%>

If you want to display a progress-bar or a loader indicating that the sync is running in the background, you have to do something like Anton suggested. As I suppose the sync needs a lot of time, I would suggest to do it with a loader and running the sync in the background.

m43nu
  • 11
  • 1
  • No I am redirecting the user on the same page. I don't really want a progress bar or something like that, just a little notification that the synchronisation has started. – Shredator May 02 '14 at 08:40
  • But how do you do the sync? In a separate task in the background or in the action called when the link you posted was clicked? If you just redirect after the sync back to the index page, you can use the code I posted above to show your flash. – m43nu May 02 '14 at 09:15
  • I do the sync by clicking the link, it does not run in the background, the user has just to wait about max five minuts, depending the content of his Dropbox. I can successfully display messages when the user is redirected but not just after he has clicked the link. – Shredator May 02 '14 at 09:52
0

There are a lot of solutions how to make it. Try the next one:

check if your application.js is loaded in layout and contains:

//= require jquery
//= require jquery_ujs

in a view:

<%= link_to 'Synchronise', {:controller => "projectusers", :action => "dropbox_sync", :id => p, :project_id => @project.identifier}, :class => "icon icon-reload", :remote => true, :confirm => "Are you sure you want to .. bla bla" %>
<p id="syncStarted" style="display:none"><strong><font color="green">Synchronisation has started, please wait...</font></strong></p>

in projectusers controller:

def dropbox_sync
  ... # action logic
  respond_to :js
end

add a view file named dropbox_sync.js.erb with only string:

$('#syncStarted').show(); 
Anton Grigoryev
  • 1,199
  • 11
  • 20
  • After confirmation, a little popup with a loading logo appears, which is nice but the `flash` used to notify the user (written in my controller) after the synchronisation do not show up anymore. Moreover, the hidden paragraph is not displayed at any time, any idea why? – Shredator May 02 '14 at 08:00
  • Show your whole dropbox_sync action. Flash uses sessions, so the flash message will show after the page reloading. I assume that you haven't page reloading and do sync via ajax. – Anton Grigoryev May 02 '14 at 08:12
  • I don't use ajax, the synchronisation (uploading files from Dropbox) is made via HTTP requests I made up with the `net/http` library then at the end of the action I have a `redirect_to :action => "index"` with some flashs – Shredator May 02 '14 at 08:17
  • If you are not using ajax, then you can't show a message while syncing. When user click the link, the page reloaded/redirected after confirm. In this case you could add a block with your flash message on the page and hide it when the new page is fully loaded – Anton Grigoryev May 02 '14 at 08:24
  • The page where the user can launch the sync is the index page, after the action he is redirected on the same page with flash messages. If I do your little trick, the message will be dislayed once the user is on the page, even if he didn't launch the sync, so it won't work. – Shredator May 02 '14 at 08:36
  • 1
    "_the message will be dislayed once the user is on the page, even if he didn't launch the sync_" You should put a message to the flash if sync has been processed and show block with message if you have any flash messages. Otherwise just don't show that block :) – Anton Grigoryev May 02 '14 at 08:45
  • Yes that's what I'm doing now, sorry maybe I misunderstood a bit your previous comment ;) – Shredator May 02 '14 at 08:47