1
votes

I am trying to make an app in Rails 4.

I am trying to set up devise with omniauth and each of Facebook, linkedin, twitter and googleauth. Im currently trying to follow this tutorial.

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

I have models called:

user.rb, profile.rb and identity.rb

The associations are:

User

has_one :profile, dependent: :destroy

has_many :identities, dependent: :destroy

Profile

belongs_to :user

Identity

belongs_to :user

Identity also validates:

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

I am trying to make a partial in my user views folder that lists out each of the optional authentication methods and shows whether a user has used them to connect or if not, providing a link to connect them.

I have made:

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

I want to show this partial in the profile edit form.

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

I can't figure out how to write the chain so that when a user updates their profile, they can connect other social media accounts to their identity.

Can anyone see where I've gone wrong?

I've tried a few different things to get these chains linked including:

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

Im just guessing with this - I can't figure it out. Can anyone see what I've done wrong?

1
ok sorry if I don't give much explanation here but hopefully you'll find answers here : github.com/charly/omnidevisable - charlysisto
Hi Charly. Thanks for this. I've been struggling to figure this out for about 3 years. I'm going to give this approach another shot before I dump it and try something else, but I've read your blog and thank you for sharing. It's in my bookmarks if I ditch everything I've tried so far and start again (which would only be about the 30th time I've done that to this point) - Mel

1 Answers

0
votes

User.identity won't work since User has_many Identities.

You may do something like @user.identities.map(&:provider).include?('facebook')