2010-01-28 34 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 
相關問題