[Ruby on Rails] Change URL id to column name

Introduction

In Rails, when you use the resources method in routes.rb, the: id parameter is usually used by default, In some cases, you may want to change the display. This time, I will describe how to change id to name column.

Target

スクリーンショット 2020-10-25 16.32.12.pngスクリーンショット 2020-10-25 16.31.38.png

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

[Ruby on Rails] How to log in with only your name and password using gem devise We will change the URL based on this.

Preparation

In the prerequisite article, the URL is mypage, so This time, I will dare to make the URL with an id.

config/routes.rb


get 'mypage', to: 'homes#mypage'
↓
get 'mypage/:id', to: 'homes#mypage', as: 'mypage'

app/controllers/users/registrations_controller.rb


def after_sign_up_path_for(resource)
  mypage_path
end
↓
def after_sign_up_path_for(resource)
  mypage_path(@user)
end

app/controllers/users/sessions_controller.rb


def after_sign_in_path_for(resource)
  mypage_path
end
↓
def after_sign_in_path_for(resource)
  mypage_path(@user)
end

app/controllers/homes_controller.rb


def mypage
end
↓
def mypage
  @user = User.find(params[:id])
end

Now you have a URL with an id.

Change id to name column

Eliminate URLs with the same name

In order to make the URL a column, it is necessary to eliminate the same name. Then apply validation.

Model validation

app/models/user.rb


validates :name, uniqueness: true

DB validation

Terminal


$ rails g migration add_index_users_name

db/migrate/xxxxxxxxxxxxx_add_index_users_name.rb


class AddIndexUsersName < ActiveRecord::Migration
  def change
    add_index :users, :name, unique: true
  end
end

Terminal


$ rails db:migrate

Actually change id to name

It is important to set find to find_by.

config/routes.rb


get 'mypage/:id', to: 'homes#mypage', as: 'mypage'
↓
get 'mypage/:name', to: 'homes#mypage', as: 'mypage'

app/controllers/homes_controller.rb


def mypage
  @user = User.find(params[:id])
end
↓
def mypage
  @user = User.find_by(name: params[:id])
end

In addition, to specify the link destination by name Use the to_param method,

app/models/user.rb


validates :name, uniqueness: true

#↓ Add
def to_param
  name
end

This should be the same as your goal.

When using resources

It is OK if you add the following to resources.

resources :users, param: :name





 Now the link destination should also be displayed by name.


# Summary
 By making it a character string instead of a number like this,
 It is easy to understand which page you are on, which makes it more convenient.
 Also, if you use a number ID in a service like SNS,
 It's not a recommended method because you'll know how many registrants you have right now.
 In that case, please try this method.

 Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so
 I would be grateful if you could follow me.
 Click here for details https://twitter.com/japwork


Recommended Posts

[Ruby on Rails] Change URL id to column name
[Ruby on Rails] How to change the column name
[Rails] How to change the column name of the table
[Ruby on Rails] From MySQL construction to database change
How to use Ruby on Rails
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
What to do when Blocked Host: "host name" appears in Ruby on Rails
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] Change the update date and creation date to your favorite notation
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Updated from time to time] Ruby on Rails Convenient methods
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
Commentary on partial! --Ruby on Rails
[Rails] Add column to devise
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
How to dynamically change the column name acquired by MyBatis
[Ruby on Rails] When parameter id acquisition does not work
[Ruby on Rails] Add a column with a foreign key constraint
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails
[Ruby on Rails] I want to get the URL of the image saved in Active Storage