dinosaurW
dinosaurW

Reputation: 113

Refresh All Information On Back Button Press: Ruby on Rails

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.

Upvotes: 1

Views: 2413

Answers (2)

ellitt
ellitt

Reputation: 833

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

Upvotes: 1

Oscar Valdez Esquea
Oscar Valdez Esquea

Reputation: 920

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!

Upvotes: 0

Related Questions