After the portfolio is completed, I implemented it additionally, so I will leave the order as a memorandum. A new introduction column will be added to the existing columns such as user name, email, and password. I will write it as simple as possible!
The order is as follows.
I will write them in order below!
Break the branch to implement new functionality.
Terminal
$ rails generate migration AddIntroductionToUsers introduction:text
invoke active_record
create db/migrate/20200712005652_add_introduction_to_users.rb
Add an introduction column with the rails g command above.
Terminal
$ docker-compose run web rails db:migrate
== 2020~~~~ AddIntroductionToUsers: migrating ===========================
-- add_column(:users, :introduction, :text)
-> 0.0518s
== 2020~~~~ AddIntroductionToUsers: migrated (0.0519s) ==================
Added to private method Add introduction attribute
users_controller.rb
def user_params_update
params.require(:user).permit(:name, :email, :image, :introduction) #Introduction added
end
Now you can update the introduction
Add validation. Enter your self-introduction within 50 characters. Feel free to set the number of characters.
user.rb
validates :introduction, presence: false, length: { maximum: 50 } #The maximum number of characters for self-introduction is 50 characters
ruby:show.html.slim
= @user.introduction
Please correct it with SCSS etc.
User editing was only user name and email, but I will add an introduction there.
ruby:edit.html.slim
.form-group
= f.label :introduction
= f.text_area :introduction, class: 'form-control', id: 'user_introduction'
This completes adding self-introduction to the users table and adding the introduction attribute. I wrote "introduce" on the way, so please be careful!
Recommended Posts