[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
How to make a follow function in Rails
Implement application function in Rails
How to implement a like feature in Ajax in Rails
Implement follow function 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
Steps to set a favicon in 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
One way to redirect_to with parameters in rails
How to easily create a pull-down in Rails
Simple notification function in Rails (only when followed)
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
How to implement image posting function using Active Storage in Ruby on Rails
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
[Rails] Implement search function
I want to use a little icon in Rails
Implement user follow function in Rails (I use Ajax) ②
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
Implement user follow function in Rails (I use Ajax) ①
[Rails 6] How to set a background image in Rails [CSS]
Rails learning How to implement search function using ActiveModel
[Rails] How to load JavaScript in a specific view
Try to implement tagging function using rails and js
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)
A story I was addicted to in Rails validation settings
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)