Rails 使用 Devise 和 Omniauth - 多个社交媒体登录提供商

Rails with Devise & Omniauth - multiple social media login providers

我正在尝试在 Rails 4.

中制作一个应用程序

我正在尝试使用 omniauth 和每个 Facebook、linkedin、twitter 和 googleauth 进行设计。我目前正在尝试学习本教程。

http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/

我的模特叫:

user.rb、profile.rb 和 identity.rb

协会是:

用户

has_one :profile, dependent: :destroy

has_many :identities, dependent: :destroy

简介

belongs_to :user

身份

belongs_to :user

身份也验证:

validates_presence_of :uid, :provider
validates_uniqueness_of :uid, :scope => :provider

我正在尝试在我的用户视图文件夹中创建一个部分,列出每个可选的身份验证方法并显示用户是否使用它们进行连接,如果没有,则提供 link 来连接它们.

我做了:

<table class="table table-bordered">

                    <tr>
                      <td><i class="fa fa-facebook"></i></td>   
                      <td>
                        <% if @profile.user.identity.provider == 'facebook' %>
                            <span class="glyphicon glyphicon-ok"</span>
                        <% else %>  
                            <%= link_to icon('Connect Facebook', id: 'facebookauth'), user_omniauth_authorize_path(:facebook) %>
                        <% end %>   

                      </td>
                    </tr>

                    <tr>
                      <td><i class="fa fa-google"></i></td> 
                      <td>
                        <% if @user.identity.provider includes 'googleauth' %>
                            <span class="glyphicon glyphicon-ok"</span>
                        <% else %>  
                            <%= link_to icon('Connect Google', id: 'googleauth'), user_omniauth_authorize_path(:google_oauth2) %>
                        <% end %>   

                      </td>
                    </tr>

                    <tr>
                      <td><i class="fa fa-linkedin"></i></td>   
                      <td>
                        <% if @user.identity.provider includes 'linkedin' %>
                            <span class="glyphicon glyphicon-ok"</span>
                        <% else %>  
                            <%= link_to icon('Connect Linkedin', id: 'linkedinauth'), user_omniauth_authorize_path(:linkedin) %>
                        <% end %>

                      </td>
                    </tr>


                    <tr>
                      <td><i class="fa fa-twitter"></i></td>    
                      <td>
                        <% if @user.identity.provider includes 'twitter' %>
                            <span class="glyphicon glyphicon-ok"</span>
                        <% else %>  
                            <%= link_to icon('Connect Twitter', id: 'twitterauth'), user_omniauth_authorize_path(:twitter) %>
                        <% end %>

                      </td>
                    </tr>
                </table>

我想在个人资料编辑表单中显示这部分内容。

<div class="formheader">Update your profile</div>

      <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <%= render 'form' %>
            <%= render 'users/authentication' %>

        </div>
      </div>    

我不知道如何编写链,以便当用户更新他们的个人资料时,他们可以将其他社交媒体帐户连接到他们的身份。

谁能看出我哪里出错了?

我尝试了一些不同的方法来获得这些链条 linked,包括:

<% if @profile.user.identity.provider == 'facebook' %>
<% if @current_user.profile.identity.provider == 'facebook' %>
<% if @user.profile.identity.provider == 'facebook' %>

我只是在猜测 - 我无法弄清楚。谁能看出我做错了什么?

User.identity 将无法工作,因为用户 has_many 身份。

你可以做类似 @user.identities.map(&:provider).include?('facebook')