简体   繁体   中英

Creating a User Edit Page with Devise

I was able to set devise up and set up a separate show page for users by creating a Users Controller. Now I want to be able to create an edit page with form fields other than email, password as it is right now. So really I want an edit profile page in addition to my edit account settings page.

Here is my code:

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"

end

changes requied in your users controller

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

Routes file changes

resources :users, only: [:show] do
  collection do
    get 'edit_profile' # this will show your view
    patch 'update_profile' # update value in database
  end
end

To update password in your custom view you must ask for current password form use. For this you must add :current_password to the permitted parameters and view.

Also instead of @user.update(user_params) you should use @user.update_with_password(user_params) . and sign in user again after update_with_password with sign_in @user, :bypass => true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM