Rails在验证用户后投票

时间:2016-08-28 02:14:06

标签: ruby-on-rails twitter-oauth acts-as-votable

我使用act-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> 

Twitter回调:

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的一部分。

提前致谢!

0 个答案:

没有答案