【问题标题】:Creating a User Edit Page with Devise使用设计创建用户编辑页面
【发布时间】:2014-12-18 05:13:26
【问题描述】:

我能够通过创建用户控制器来设置设计并为用户设置单独的显示页面。 现在我希望能够创建一个编辑页面,其中包含电子邮件以外的表单字段,密码就像现在一样。所以除了我的编辑帐户设置页面之外,我真的想要一个编辑个人资料页面。

这是我的代码:

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"

结束

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise


    【解决方案1】:

    您的用户控制器需要更改

    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_with_password(user_params),而不是@user.update(user_params)。并在update_with_password 之后使用sign_in @user, :bypass =&gt; true 再次登录用户

    【讨论】:

    • 谢谢迪帕克。答案有效,现在我可以拥有编辑个人资料页面。太棒了!
    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多