2010-01-28 88 views
0

我有以下型号:问题与accepts_nested_attributes_for同时使用authlogic_oauth

class Merchant 
    acts_as_authentic 
    has_one :store 
    accepts_nested_attributes_for :store 
end 

class Store 
    belongs_to :merchant 
end 

我使用authlogic_oauth宝石Twitter的认证。在注册时,我保存商家和商店模型。如果我禁用oauth认证,则保存这两个模型。当我启用oauth身份验证时,只保存商户实例。

花了一些时间看看authlogic_oauth宝石代码后,我认为找到了罪魁祸首。 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 

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

回答

0

我通过在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