2013-08-21 59 views
3

我有一個名爲password和phone_number的用戶模型。在創建用戶時,我需要電子郵件和密碼,但我需要一個表格另一個頁面添加電話號碼!做一個form_for將數據添加到現有模型(導軌)

我嘗試這樣做:

= form_for @user do |f| 
    = f.label :phone_number 
    = f.text_field :phone_number 
    %p.button 
    = f.submit 

問題的是,它擊中其詢問password_reset用戶更新:

users_controller.rb

def update 
    if current_user 
    @user = current_user 
    else 
    @user = User.find(params[:id]) 
    end 
    @user.password_hash = nil 
    if @user.update_attributes(params[:user]) 
    redirect_to @user, :notice => "Password has been changed!" 
    else 
    render "edit" 
    end 
end 

我怎麼能修復這個?

+1

我不明白這個問題,是不是正確更新了?什麼是password_reset? – Roy

+0

Roy!怎麼了大聲笑。是的,密碼重置工作正常,但我只是試圖讓它在一個新的形式添加phone_number。當我提交我得到的錯誤'密碼不能爲空' –

+0

我想我正在尋找一種方法,使用戶更新時,還添加phone_number當在該頁 –

回答

3

可能是你可以決定基於PARAMS更新操作。

def update 
    if current_user 
    @user = current_user 
    else 
    @user = User.find(params[:id]) 
    end 

    if params[:user][:password] 
    # or other actions will save new password? 
    @user.password_hash = nil 
    notice = "Password has been changed!" 
    else 
    notice = "User profile has been changed!" 
    end 

    if @user.update_attributes(params[:user]) 
    redirect_to @user, :notice => notice 
    else 
    render "edit" 
    end 

end 
+0

感謝您的答案。 –

1

您可以存儲在會話編輯電話號碼,然後讀取它,當你的主編輯表單提交

1

關於第一個表格,你可以得到電子郵件地址和密碼的細節,當你提交表單不創建用戶記錄,而不是存儲電子郵件和密碼閃光燈[:用戶] = {:電子郵件=>「給定的電子郵件」,:密碼=>「給定的密碼」}(閃光燈會自動重置,一旦它被重定向到下一頁,所以在這種情況下,閃光燈會比會議更好,但去會議,如果你想有一個在第二個表單上選擇返回選項),然後重定向到第二個表單。

關於第二個表格就可以得到電話號碼細節與添加兩個隱藏域的電子郵件和密碼,你可以填補閃光值一起[:用戶]或會話[:用戶]。當你提交你的第二個表單時,你可以創建一個新的用戶,你的params [:user]應該有email,password和phone_number。

相關問題