2012-10-15 53 views
0

我在我的API中切換到http基本授權以使測試更容易,但是現在我無法獲得user_id ...以下是我的ThingController中與devise配合使用的代碼:用http基本授權分配user_id

def create 
    @thing = Thing.new(params[:thing]) 
    @thing.user_id = current_user.id 
    @thing.save 
    if @thing.save 
     redirect_to user_path 
    else 
     render :new 
    end 
    end 

現在,這使我有以下錯誤:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

和它引用的行

@thing.user_id = current_user.id 

我不知道如何在創建時將user_id分配給我的東西。任何幫助將不勝感激。

+0

的東西看看在下面的答案並看看是否有幫助。 http://stackoverflow.com/questions/7554783/called-id-for-nil-in-rails-3 –

+0

我知道nil的id == 4.這不是我的問題。我在問怎麼不把nil的id分配給我的thing.user_id。現在它沒有指向任何東西。我需要找到一種方法來使用基本身份驗證登錄時保存當前用戶的ID。 –

回答