今天上線發現master更新了模板,就拿來用了一下,重新修改了之前側邊欄的class和id來適配新的模板,並且在每個平臺後臺的子頁面中都填上了側邊欄的渲染,效果還不錯:
之前的醜陋界面成功變成了這個樣子,再改改提示文字,字體樣式什麼也用上模板中的按鈕和label就完美了。
主要工作是做好了修改賬戶的功能。
讀了一下user部分的代碼,學會了params之類的使用方法,因爲admin也是個user,所以直接採用了user_param來輔助管理員信息的更新。
更新界面在修繕好的模板中直接插入了一個form:
大概就是這樣。其中,用戶採用了session_helper中的current_user方法拿到,(另外驗證需要將來寫一下必須是admin類型),表單中大部分可見屬性都使用了當前用戶的值作爲默認值,這樣同時可以一定程度上代替展示頁面(雖然管理員可能不怎麼需要),不過有關密碼的三個輸入框需要用戶來填入。這裏採用了一些驗證機制,利用了.authenticate方法來驗證身份纔可以進行更新,同時可以不輸入新密碼,這樣就不會修改密碼,如果輸入了新密碼則要確認一遍纔可以更新。邏輯實現如下:
def update
@admin = User.find(params[:user][:id])
confirm_id = @admin.authenticate(params[:user][:password_old])
same_new_password = true
if !params[:user][:password_new].nil? && !params[:user][:password_confirmed].nil? && params[:user][:password_new] != params[:user][:password_cofirmed]
same_new_password = false
end
respond_to do |format|
if confirm_id == false
format.html { redirect_to admin_edit_path(@admin), notice: "認證失敗" }
elsif same_new_password == false
format.html { redirect_to admin_edit_path(@admin), notice: "新密碼不一致" }
else
up = user_param
up[:password] = params[:user][:password_old]
if @admin.update_attributes(up)
# flash[:success] = "success to update user information!"
# redirect_to 'admins/own_space'
format.html { redirect_to admin_edit_path(@admin), notice: "#{@admin.username}\'s information was successfully updated." }
else
format.html { redirect_to admin_edit_path(@admin), notice: "資料格式錯誤" }
end
end
end
end
修改之後跳轉回本頁面並刷新提示信息,不成功會報各種錯誤。效果如下: