1

我正在使用acts-as-votable gem 来允许用户对项目进行投票。如果用户未登录,upvote 按钮会发送用户通过 Twitter 登录,但不会记录投票。

我想允许用户登录并通过相同的点击提交投票。

记录投票或登录用户的逻辑:

          <!-- 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 %>

像部分:

<%= 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> 

推特回调:

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

会话控制器:

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

结尾

我认为这需要与传递要喜欢的项目的变量有关,然后喜欢该项目作为 session#create 的一部分。

提前致谢!

4

0 回答 0