[RUBY] TECH CAMP learning personal application creation ③

Addition of user information editing function

First, enter the following code at the terminal.

terminal.


$ rails g controller users

Then enter the following in the routing.

routes.rb


 devise_for :users
 root "photos#index"
 resources :users, only: [:edit, :update]

The meaning of resources means resources in Japanese, and I think it can be interpreted as using the editing and updating functions in users.

Then edit the controller.

users_controller.rb


def edit
end

def update
   if current_user.update(user_params)
      redirect_to root_path
   else
      render :edit
   end
end

private

def user_params
   params.require(:user).permit(:name, :email)
end

(1) current_user is a helper method of devise, and you can get the user information during login. (2) redirect_to transfers to a path different from the originally received path. The above means forwarding to root_path. ③ If the above fails, render will call edit. ④ private is a private method that cannot be called from outside the class. The merit -Isolate methods that are in trouble if called from outside the class -Readability, when searching for a method called from outside the class, you do not have to read the part below private. (5) user_params The following are strong parameters that receive only parameters with the specified key. ⑥ Request user in the strong parameter to get permission for: name ,: email. Use binding.pry immediately after update to see the details.

Recommended Posts

TECH CAMP learning personal application creation ③
TECH CAMP learning personal application creation ②
☆ First post: TECH CAMP learning, personal application creation ①, fledgling engineer
Personal application creation # 2
Personal application creation # 3
Personal application creation # 1
TECH CAMP learning personal app ④ git ignore
Personal application creation term
[Personal application creation] Information management application production 16th day
[Personal application creation] Information management application production 14th day
2020/10/1 Tech Camp Day 11 Output
2020/10/3 Tech Camp Day 13 Output
Personal application development diary # 2
2020/10/2 Tech Camp Day 12 Output
Personal application production diary 3
2020/10/4 Tech Camp Day 14 Output
Personal application development diary # 3
TECH CAMP After 10 weeks
[Spring Boot] Web application creation
Ruby Learning # 99 Personal Programming Notebook
[Impression] Take a Tech Camp
Task management application creation procedure