1

My RoR website implements a calendar using the fullcalendar gem. Each user has a calendar that shows the events which he or she is attending. The following scenario sets the stage for my question:

  1. User Alice clicks an event on her calendar, which leads to the event's show page.
  2. Alice clicks the 'unattend event' button on the event's show page
  3. Alice clicks the back button.
  4. The event that she just unattended is still listed on her calendar.

How can I make the browser refresh Alice's event information, so that only events that she is currently attending are displayed, when the back button is pressed?

I've scoured Stack Overflow and not found a working solution. Cache clearing as seen here How to prevent browser page caching in Rails did not work. I'm not even sure this is a caching problem. Perhaps there is some javascript that will force information to be reloaded? Any solutions are much appreciated. Thanks!

In response to @ElliottRoche:

The attend button is clicked in the view, triggering the attend action:

<%= button_to button_text, attend_event_path(@event), :remote => true,  :id => "attend" %>

The attend action adds/removes the attendee from the list of attendees as follows:

 def attend

    @event = Event.find(params[:id])
    @attendee = User.find(session[:user_id])

    if @event.users.exclude?(@attendee)
      @event.users << @attendee
      @event.save

    else
      @event.users.delete(@attendee)
    end

    @attendees = @event.users


    respond_to do |format|
      format.js
      format.html {  }
      format.json { }
    end
end

With attend.js.erb, the attendees list is updated using UJS:

$('#attendees-list').html("<%= j render(:partial => 'attendees_list', :locals => { :attendees => @attendees }) %>");

From here, when the back-button is pressed, the fact that the user has attended/unattended the event is not reflected.

Community
  • 1
  • 1
dinosaurW
  • 113
  • 2
  • 10
  • Could it be a `fullcalendar` caching issue? Because if the event is really "unattended" then this definitely looks like a caching issue. – Technoh Mar 23 '16 at 20:30
  • Does it change if you hit reload? I understand hitting back would not show the "updated" calendar without using some javascript or something, but what happens after hitting the reload button (after hitting back)? – Oscar Valdez Esquea Mar 23 '16 at 20:33
  • I've also had this problem with other areas of my site not related to full calendar. Basically, when the back button is pressed after a page is updated with Javascript (like adding an attendee), the information is not up to date on the newly displayed page. I've also tried `Rails.cache.clear` but this did not work. Perhaps it is a `turbolinks` problem. Any ideas for help? – dinosaurW Mar 23 '16 at 20:34
  • @OscarValdezEsquea The page does update when I hit reload. – dinosaurW Mar 23 '16 at 20:36
  • Is there a Rails controller that's doing the update? If so, does it do any kind of redirect? – ellitt Mar 23 '16 at 20:38
  • @ElliottRoche The update is done with `UJS`, when the `unattend` button is clicked the user is removed from the events users (attendees) with `@event.users.delete(@attendee)` follow by a `respond_to` block that updates the page with the new list of attendees. – dinosaurW Mar 23 '16 at 20:46
  • Would it be possible to see that part of the code? – ellitt Mar 23 '16 at 20:52
  • @ElliottRoche. Please see my edit – dinosaurW Mar 23 '16 at 21:00

2 Answers2

1

Try adding format.html { redirect_to(@event) } in the respond to block.

ellitt
  • 787
  • 5
  • 17
0

Reason is you are hitting the back button and the page is taking you to the last page you were at, restoring the state it was at when you left it. Hitting the back button doesn't force a page "reload". What you could is configure the "back" button and force it to instead issue a redirect OR render of the page you want to go to (passing over the parameters if necessary).

There's some new methods introduced in HTML5. It may be helpful and it'd likely fix your problem, but I can't guarantee it will as I have not tried it myself.

https://css-tricks.com/using-the-html5-history-api/

Good luck!

Oscar Valdez Esquea
  • 890
  • 1
  • 11
  • 26