Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6060581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:50:32+00:00 2026-05-23T08:50:32+00:00

I followed Railscasts #235 and #236 to setup creating user authentications with omniauth. http://railscasts.com/episodes/235-omniauth-part-1

  • 0

I followed Railscasts #235 and #236 to setup creating user authentications with omniauth.
http://railscasts.com/episodes/235-omniauth-part-1
http://railscasts.com/episodes/236-omniauth-part-2

I have a 2 boolean attributes on the user model called :facebok_share and :twitter_share that I want to set to true when a new authentication is created.

I have this working for me when I create a new user, but if an existing user adds an authentication I cannot get the boolean to update to true.

When apply_omniauth(omniauth) is called it sets self.facebook_share = true or self.twitter_share = true in my user model.

I’ve tried to add a new method called apply_share which changes the booleans depending on provider, and I’m trying to call current_user.apply_share(omniauth) but nothing is happening in the database.

What am I doing wrong? Thanks!

## authentications controller

class AuthenticationsController < ApplicationController

  def index
    @title = "Authentications"
    @authentications = current_user.authentications if current_user
  end

  def create
    # creates omniauth hash and looks for an previously established authentication
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    # if previous authentication found, sign in user
    if authentication
      flash[:notice] = "Signed in successfully"
      sign_in_and_redirect(:user, authentication.user)
    #  for users already signed in (current_user), create a new authentication for the user
    elsif current_user
      current_user.apply_share(omniauth)
      current_user.authentications.create(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => (omniauth['credentials']['token'] rescue nil),
                                           :secret => (omniauth['credentials']['secret'] rescue nil))
      flash[:notice] = "authentications successful"
      redirect_to authentications_url
    # new user is created and authentications are built through apply_omniauth(omniauth)
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        flash[:notice] = "Signed in successfully"
        sign_in_and_redirect(:user, user)
      # if validations fail to save user, redirects to new user registration page 
      # new twitter authentications redirect so user can enter their password
      else
        session[:omniauth] = omniauth
        redirect_to new_user_registration_url
      end
     end
   end

  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication."
    redirect_to authentications_url
  end

end

## user model

 # set share booleans to true depending on 'provider' type
  def apply_share(omniauth)
    case omniauth['provider']
      when 'facebook'
        self.facebook_share = true
      when 'twitter'
        self.twitter_share = true
     end
   end

 # from authentications controller, new user split into type of provider
 def apply_omniauth(omniauth)
   case omniauth['provider']
   when 'facebook'
     self.apply_facebook(omniauth)
   when 'twitter'
     self.apply_twitter(omniauth)
   end
   # builds authentication with provider, uid, token, and secret
   authentications.build(hash_from_omniauth(omniauth))
  end

 protected

 # sets new user attributes from facebook
 def apply_facebook(omniauth)
   self.name = omniauth['user_info']['name']
   self.email = omniauth['user_info']['email'] if email.blank?
   self.facebook_share = true
 end

 # sets new user attributes from twitter 
 def apply_twitter(omniauth)
   if (extra = omniauth['extra']['user_hash'] rescue false)
     # Example fetching extra data. Needs migration to User model:
     # self.firstname = (extra['name'] rescue '')
     self.name = (extra['name'] rescue '')
     self.bio = (extra['description'] rescue '') 
   end
   self.twitter_share = true

 end

 # set authentication attributes to those from 'omniauth' hash
 def hash_from_omniauth(omniauth)
   {
     :provider => omniauth['provider'],
     :uid => omniauth['uid'],
     :token => (omniauth['credentials']['token'] rescue nil),
     :secret => (omniauth['credentials']['secret'] rescue nil)
   }
 end
end


## new methid with :before add => :apply_share
def apply_share(authentication) 
  case authentication['provider'] 
    when 'facebook' 
      self.facebook_share = true 
    when 'twitter'
      self.twitter_share = true 
    end 
  self.save
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:50:32+00:00Added an answer on May 23, 2026 at 8:50 am

    I believe your never actually saving current_user. So your setting your attributes to true, and then redirecting. The association is stored in the authentication model, so Rails, trying to be helpful, doesn’t update current_user, just the new instance of authentication

    try:

    current_user.apply_share(omniauth)
    current_user.save
    

    and see if that fixes it. Now if it does, I would strongly recommend using a callback instead. Take a look here:

    http://guides.rubyonrails.org/association_basics.html

    Section 4.5 about association callbacks. You can do a before_add callback on your has_many authentications assocation to remove that code from your controller as its getting pretty bloated as is.

       class User < ActiveRecord::Base
         has_many :authentications, :before_add => :apply_share
    
         def apply_share(authentication)
          #update attributes
          #save model
         end
       end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed Ryan Bates Omniauth Part1 railscats http://railscasts.com/episodes/235-omniauth-part-1 . I put twitter and Facebook
I followed Railscasts(ASCII versions) #235 and and part of #236 to setup creating user
I followed the rails cast of omniauth to create authentication for twitter ( http://railscasts.com/episodes/235-omniauth-part-1?view=comments
I have followed the instructions on http://railscasts.com/episodes/147-sortable-lists for creating a sortable list. The list
I followed RailsCasts authentication from scratch (http://railscasts.com/episodes/250-authentication-from-scratch-revised) using the bcrypt-ruby gem and have the
I followed the instructions in Railscasts #197 http://railscasts.com/episodes/197-nested-model-form-part-2 about how to add links that
Has anyone managed to make the example at http://railscasts.com/episodes/196-nested-model-form-part-1 work? When I followed through
I recently implemented Authlogic to my project for authentication. I followed http://railscasts.com/episodes/160-authlogic and had
I followed the tutorial http://railscasts.com/episodes/253-carrierwave-file-uploads?view=asciicast and I've got my nested form setup with the
I've just followed the Railscast tutorial: http://railscasts.com/episodes/262-trees-with-ancestry Is it possible to paginate results from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.