[Rails] A simple way to implement a self-introduction function in your profile

Implement self-introduction function in profile

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!

how?

The order is as follows.

  1. git checkout -b introduction
  2. Add an introduction column to the users table
  3. Reflect in the database
  4. Add the required code to the users controller
  5. Add necessary code to user model
  6. Display on the user details page (MY PAGE)
  7. Display on the user edit page (EDIT)
  8. Completed!

I will write them in order below!

1. git checkout -b introduction

Break the branch to implement new functionality.

2. Add an introduction column to the users table

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.

3. Reflect in the database

Terminal


$ docker-compose run  web rails db:migrate
== 2020~~~~ AddIntroductionToUsers: migrating ===========================
-- add_column(:users, :introduction, :text)
   -> 0.0518s
== 2020~~~~ AddIntroductionToUsers: migrated (0.0519s) ==================

4. Add the required code to the users controller

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

5. Add necessary code to user model

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

6. Display on the user details page (MY PAGE)

ruby:show.html.slim


 = @user.introduction

Please correct it with SCSS etc.

7. Display on the user edit page (EDIT)

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'

Complete

This completes adding self-introduction to the users table and adding the introduction attribute. I wrote "introduce" on the way, so please be careful!

スクリーンショット 2020-07-12 13.33.19.png

Recommended Posts

[Rails] A simple way to implement a self-introduction function in your profile
Implement simple login function in Rails
How to implement a like feature in Rails
Implement application function in Rails
How to implement a like feature in Ajax in Rails
I want to define a function in Rails Console
How to implement a circular profile image in Rails using CarrierWave and R Magick
Add a search function in Rails.
3 Implement a simple interpreter in Java
Implement a contact form in Rails
Implement CSV download function in Rails
I tried to implement Ajax processing of like function in Rails
How to get JDK 11 on your mac in a comfortable way
How to implement search functionality in Rails
How to insert a video in Rails
[Rails withdrawal] Create a simple withdrawal function with rails
How to implement ranking functionality in Rails
How to implement a slideshow using slick in Rails (one by one & multiple by one)
I wanted to implement a slide show in a fashionable way with slick.
"Teacher, I want to implement a login function in Spring" ① Hello World
Convert to a tag to URL string in Rails
Add a tag function to Rails. Use acts-as-taggable-on
Implement star rating function using Raty in Rails6
[Android] Implement a function to display passwords quickly
How to easily create a pull-down in Rails
Simple notification function in Rails (only when followed)
How to implement guest login in 5 minutes in rails portfolio
Implement post search function in Rails application (where method)
Implement a reservation system using Rails and simple calendar! Let's add validation to datetime!
[Rails] Implement credit card registration / deletion function in PAY.JP
Introduce devise in Rails to implement user management functionality
I want to use a little icon in Rails
Throw raw SQL to a read replica in Rails
I tried to make a login function in Java
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
Implement markdown in Rails
What to do if you get a "302" error in your controller unit test code in Rails
I want to implement a product information editing function ~ part1 ~
Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
How to deploy jQuery in your Rails app using Webpacker
How to display a graph in Ruby on Rails (LazyHighChart)
Establish a search bar in Rails ~ Join multiple tables to search
I tried to implement a buggy web application in Kotlin
Apply CSS to a specific View in Ruby on Rails
Implement login function simply with name and password in Rails (3)
Implement user registration function and corporate registration function separately in Rails devise
[Rails] Implement User search function
Implement LTI authentication in Rails
2 Implement simple parsing in Java
[Rails] How to implement scraping
[Rails] Implement image posting function
A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019
I tried to create a simple map app in Android Studio
Implement a refined search function for multiple models without Rails5 gem.
I want to add a browsing function with ruby on rails
[Implementation procedure] Create a user authentication function using sorcery in Rails
How to implement a job that uses Java API in JobScheduler