[英]Creating a User Edit Page with Devise
通過創建用戶控制器,我能夠為用戶設置並設置單獨的顯示頁面。 現在,我希望能夠使用當前不是電子郵件,密碼的表單域創建一個編輯頁面。 因此,除了我的“編輯帳戶設置”頁面外,我真的還需要一個“編輯個人資料”頁面。
這是我的代碼:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
# Config/routes
Rails.application.routes.draw do
devise_for :users
get 'users/:id' => 'users#show', as: :user
get 'users/editprofile'
resources :users, :only => [:show]
resources :postings
root 'postings#index'
get 'pages/about'
get 'pages/contact'
get 'pages/blog'
# schema
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "preferred_work_type"
t.string "preferred_location"
t.string "preferred_pay_rate"
t.string "preferred_environment"
t.string "company_name"
t.string "company_title"
t.string "company_employment_date"
t.string "company_work_environment"
t.string "college_school_name"
t.string "college_major"
t.string "college_date"
t.string "college_degree"
t.string "course_name"
t.string "course_date"
t.string "primary_skills"
t.string "secondary_skills"
t.string "activity_name"
t.string "activity_date"
t.text "interview_questions"
t.string "professional_reference_name"
t.string "professional_reference_relationship"
t.string "professional_reference_phone_number"
t.string "preferred_text_editor"
t.string "first_name"
t.string "last_name"
結束
class UsersController < ApplicationController
before_filter :authenticate_user!
def edit_profile
@user = current_user
end
def update_profile
@user = User.find(current_user.id)
if @user.update(user_params)
redirect_to root_path
else
render "edit"
end
end
private
def user_params
params.required(:user).permit(:xxx, :yyy)
end
end
resources :users, only: [:show] do
collection do
get 'edit_profile' # this will show your view
patch 'update_profile' # update value in database
end
end
要在您的自定義視圖中更新密碼,您必須要求使用當前的密碼表單。 為此,必須在允許的參數和視圖中添加:current_password
。
此外,而不是@user.update(user_params)
你應該使用@user.update_with_password(user_params)
並登錄用戶之后再次update_with_password
與sign_in @user, :bypass => true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.