代码之家  ›  专栏  ›  技术社区  ›  Harish Shetty

使用AuthLogic时的接受嵌套属性问题

  •  0
  • Harish Shetty  · 技术社区  · 15 年前

    我有以下型号:

    class Merchant
      acts_as_authentic
      has_one :store
      accepts_nested_attributes_for :store
    end
    
    class Store
      belongs_to :merchant
    end
    

    我正在使用AuthLogic_oauth gem进行Twitter身份验证。注册时,我会保存商家和商店模型。如果禁用OAuth身份验证,两个模型都将被保存。当我启用OAuth身份验证时,只保存商家实例。

    在花了一段时间看了AuthLogic_oauth gem代码之后,我认为找到了罪魁祸首。AuthLogic_OAuth gem在OAuth调用期间将ActiveRecord属性存储在会话中。但它不存储关联的属性。

    # authlogic_oauth : lib/authlogic_oauth/acts_as_authentic.rb
    def save(perform_validation = true, &block)
      if perform_validation && block_given? && redirecting_to_oauth_server?
        # My comment: Any nested attributes are not saved in the session
        session_class.controller.session[:authlogic_oauth_attributes] = attributes.reject!{|k, v| v.blank?}
        # some code
      end
      # some code
    end
    

    我可以破解gem代码,但我想知道是否有更好的解决方案。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Harish Shetty    15 年前

    我通过在OAuth调用期间的会话中临时保存存储属性来解决这个问题。我希望有更好的方法来解决这个问题。

    class MerchantsController < ApplicationController
     before_filter :init_nested_attr
    
     def create
       @merchant = Merchant.new(params[:merchant])
       @merhcant.save do |result|
         #some code
       end
     end
    
    private
     def init_nested_attr
       if session[:authlogic_oauth_attributes]
         params[:merchant] = session[:authlogic_oauth_attributes]
         params[:merchant][:store_attributes] = session.delete(:authlogic_oauth_store_attributes)
       else
         session[:authlogic_oauth_store_attributes] = params[:merchant][:store_attributes]
       end
     end
    end
    
    推荐文章