0

Background: I'm using the acts_as_votable gem to log likes on a Resource model. I am working on splitting the behavior of the like button to accommodate (a) users who are signed in and (b) users who are not signed in.

before/after actions in the resource controller

before_action :set_resource, only: [:show, :edit, :update, :destroy]
before_action :save_original_path, only: [:like]
before_action :require_signin!, only: [:like]
after_action :clear_original_path, only: [:like]

If the user is not signed in, and they click the like button, I store their original path --> force them to sign in --> return to their original path (liking the resource) --> and redirect them back to the resource index.

If the user is signed in, ajax is simply used to record the vote and refresh the page.

like action in resources_controller.rb

def like
  @resource = Resource.find(params[:id])
  @resource.liked_by current_user
  if session[:return_to] != nil
    redirect_to resources_path
  else
    respond_to do |format|
      format.html
      format.js
    end
  end
end

Something funky is happening with the if statement in the like action above. The redirect_to resources_path flow is working perfectly for users who are not signed in. The respond_to ajax call, however, is not refreshing the page.

If I move the respond_to call out of the if statement and delete the if statement, then it works just fine.

Any ideas?

rcrusoe
  • 441
  • 2
  • 4
  • 14

1 Answers1

0

Got it working. Unsurprisingly, I was making it too complicated. This works:

def like
  @resource = Resource.find(params[:id])
  @resource.liked_by current_user
  respond_to do |format|
    if session[:return_to] != nil
      format.html { redirect_to(resources_path) }
    end
    format.html
    format.js
  end
end
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
rcrusoe
  • 441
  • 2
  • 4
  • 14