Rails 3:创建身份验证时更新用户属性

发布于 2024-11-17 06:17:06 字数 4366 浏览 4 评论 0原文

我按照 Railscasts #235 和 #236 设置使用omniauth 创建用户身份验证。 http://railscasts.com/episodes/235-omniauth-part-1 http://railscasts.com/episodes/236-omniauth-part-2

我在用户模型上有两个布尔属性,名为 :facebok_share 和 :twitter_share ,我想在创建新的身份验证时将其设置为 true 。

当我创建新用户时,我可以使用此功能,但是如果现有用户添加身份验证,我无法将布尔值更新为 true。

当 apply_omniauth(omniauth) 被调用时,它在我的用户模型中设置 self.facebook_share = true 或 self.twitter_share = true 。

我尝试添加一个名为 apply_share 的新方法,该方法根据提供程序更改布尔值,并且我尝试调用 current_user.apply_share(omniauth) 但数据库中没有任何反应。

我做错了什么?谢谢!

## 身份验证控制器

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘曦 2024-11-24 06:17:06

我相信你从未真正保存过 current_user。因此,您将属性设置为 true,然后重定向。该关联存储在身份验证模型中,因此 Rails 试图提供帮助,不会更新 current_user,只是更新身份验证的新实例

try:

current_user.apply_share(omniauth)
current_user.save

并查看是否修复了该问题。现在,如果确实如此,我强烈建议使用回调。请看这里:

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

第 4.5 节有关关联回调。您可以在 has_many 身份验证关联上执行 before_add 回调,以从控制器中删除该代码,因为它变得相当臃肿。

   class User < ActiveRecord::Base
     has_many :authentications, :before_add => :apply_share

     def apply_share(authentication)
      #update attributes
      #save model
     end
   end

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
放赐 2024-11-24 06:17:06

设置*_share 属性后,您需要对User 对象调用#save

has_many 集合添加新项目会自动保存该集合项目,但不会触发父级 (belongs_to) 上的保存操作。

You need to call #save on the User object after setting the *_share attributes.

Adding new items to a has_many collection automatically saves the collection item, but does not trigger a save operation on the parent (belongs_to).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文