我能够通过创建用户控制器来设置设计并为用户设置单独的显示页面。 现在我希望能够创建一个包含电子邮件,密码以外的表单字段的编辑页面。除了我的编辑帐户设置页面,我真的想要一个编辑个人资料页面。
这是我的代码:
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"
端
答案 0 :(得分:0)
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
再次登录用户