1

I'm using the acts-as-votable gem to allow users to vote on items. If a user is not signed in, the upvote button sends the user to sign in via Twitter, but the vote is not recorded.

I would like to allow a user to sign in and submit a vote with the same click.

Logic to either record a vote or sign the user in:

          <!-- Like/Unlike Button -->
          <% if current_user %>
            <!-- If the user is signed in, allow them to vote -->
            <div class="votes">
              <% if (current_user.liked? resource) %>
                <!-- Unlike -->
                <%= render(:partial => 'unlike', :locals => {:resource => resource})%>
              <% else %>
                <!-- Like -->
                <%= render(:partial => 'like', :locals => {:resource => resource})%>
              <% end %>
            </div>
          <% else %>

            <!-- If the user is not signed in, send them to Twitter -->
            <%= link_to "/auth/twitter" do %>
              <button class="ui basic compact icon right floated button" data-tooltip="You need to sign in before saving this link." data-variation="basic" data-position="top right">
                <i class="heart icon" style="color: #EDB0B1"></i> <%= resource.get_likes.size %>
              </button> 
            <% end %>
          <% end %>

Like partial:

<%= link_to like_resource_path(resource), method: :get, remote: true, class: 'like_resource' do %>
<button class="ui basic compact icon right floated button vote_count">
    <i class="heart icon" style="color: #F1F1F1"></i> <%= resource.get_likes.size %>
</button> 

Twitter Callback:

get 'auth/twitter/callback', to: 'sessions#create'

Sessions controller:

class SessionsController < ApplicationController
def create
  auth = request.env["omniauth.auth"]
  user = User.from_omniauth(auth)
  session[:user_id] = user.id
  redirect_to user, :notice => "Signed in!"
end

def destroy
  session[:user_id] = nil
  redirect_to root_url, :notice => "Signed out!"
end

end

I assume this requires something to do with passing a variable for the item to be liked, then liking that item as part of session#create.

Thanks in advance!

rcrusoe
  • 441
  • 2
  • 4
  • 14
  • Just FYI, you are writing Rails using some very old idioms. Your `render` calls don't need to look like this: `<%= render(:partial => 'unlike', :locals => {:resource => resource})%>`, the preferred way for a long time has been `<%= render 'unlike', resource: resource %>`. – user229044 Aug 28 '16 at 03:38
  • Thanks @meagar. That's a product of me learning to write rails using assorted stack overflow Q&A while I hack together this project. – rcrusoe Aug 30 '16 at 03:33
  • Any advice on how to solve the posted question? – rcrusoe Aug 30 '16 at 03:34

0 Answers0