일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- jsp
- 건담
- 一日一つメソッド
- 자바
- CSS
- 비즈니스일본어
- 반다이몰
- 일본어
- springboot
- Flutter
- メソッド
- C로 시작하는 컴퓨터 프로그래밍4판
- Web
- html
- 日本語
- vscode
- java
- Spring
- 인프런
- DART
- 単語
- 연습문제
- 건담베이스
- ruby
- rails
- nico
- rails7
- Python
- 디지몬
- javascript
Archives
- Today
- Total
AR삽질러
Rails 一日一つメソッド (Day07) updateメソッド 본문
728x90
Rails 一日一つメソッド (Day7) updateメソッド
updateメソッドを使えば、既存のオブジェクトの属性を修正しDatabaseに保存でくる。
updateメソッドはActiveRecordで使われオブジェクトの色んな属性わ一気にUpdateできる。
updateメソッド動作方法
1. modle探し
- updateするオブジェクトを探す。
user = User.find(1)
2. 属性Update
- 探したオブジェクトの属性を'update'メソッドを使ってupdateする。
user.update(name: "変えたい名前", email: "変えたいEmail")
3. 確認
- updateメソッドが成功したら true
- updateメソッドが失敗したら false
updateメソッド例題
users_controller
class UsersController < ApplicationController
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_param)
redirect_to users_path, notice: '회원 정보가 수정되었습니다.'
else
render :edit, status: :unprocessable_entity
end
end
private
def user_param
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
routes.rb
Rails.application.routes.draw do
get 'users', to: 'users#index'
get 'users/:id', to: 'users#user_show', as: 'user'
get 'user_search', to: 'users#user_search', as: 'user_search'
# 회원가입
get 'signup', to: 'users#new', as: 'signup'
post 'users', to: 'users#create'
# 회원삭제
delete 'users/:id', to: 'users#destroy'
# 회원수정
get 'users/:id/edit', to: 'users#edit', as: 'edit_user'
patch 'users/:id', to: 'users#update', as: 'update_user'
end
index.html.erb
<!-- app/views/users/index.html.erb -->
<h1>登録されている Users</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td>
<%= link_to '수정', edit_user_path(user), class: 'action-button' %><br>
<%= button_to '삭제', user_path(user), method: :delete, class: 'action-button' %>
</td>
</tr>
<% end %>
</tbody>
</table>
edit.html.erb
<!-- app/views/users/edit.html.erb -->
<h1>User情報修正</h1>
<%= form_with(model: @user, url: update_user_path(@user), method: :patch, local: true) do |form| %>
<div class="field">
<%= form.label :name %><br>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :email %><br>
<%= form.email_field :email %>
</div>
<div class="actions">
<%= form.submit "Update" %>
</div>
<% end %>
https://github.com/designAR/rails_method_study/tree/day06_update
728x90
반응형
LIST
'Ruby > RubyOnRails-日本語' 카테고리의 다른 글
Rails 一日一つメソッド (Day09) authenticateメソッド (0) | 2023.12.29 |
---|---|
Rails 一日一つメソッド (Day08) has_secure_passwordメソッド (0) | 2023.12.28 |
初めてのRuby on Rails ルーティング、アクション、View, ERB (2) (1) | 2023.12.27 |
初めてのRuby on Rails Controller作成, トラブルシューティング (1) (0) | 2023.12.27 |
Rails 一日一つメソッド (Day6) new, persisted?メソッド (0) | 2023.12.26 |