簡體   English   中英

使用authlogic_oauth時accepts_nested_attributes_for的問題

[英]Issue with accepts_nested_attributes_for while using authlogic_oauth

我有以下型號:

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身份驗證時,只會保存Merchant實例。

花了一些時間查看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代碼,但我想知道是否有更好的解決方案。

我通過在Oauth調用期間在會話中臨時保存Store屬性來解決此問題,希望有更好的方法來解決此問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM