List of copy and paste collections that are useful when creating apps with Ruby on Rails

When creating an app with Rails, I've put together some code that I found useful in the early stages of creation (for myself).

Install Ruby on Rails

% rails _6.0.0_ new 'file name' -d mysql

Things to set before creating a database

config/database.yml


# encoding: utf8mb4
=> encoding: utf8

I can't explain in detail yet, but if it remains utf8mb4, it cannot be migrated well due to the number of bytes. Please refer to here for the correspondence when creating a database with utf8mb4. 【MySQL】Mysql2::Error: Specified key was too long; max key length is 767 bytes

If you make a mistake in setting the database and create a database
  • Delete the database
% rails db:drop
% rails db:create

Gem you want to install

gemfile.rb


gem 'devise'             #Implementation of user registration function
gem 'mysql2', '>= 0.5.3' #Change mysql version
gem 'pry-rails'          #Used when checking for errors and testing
gem 'mini_magick'        #Gem for using ImageMagick
gem 'image_processing'   #Gem to adjust image size
gem 'active_hash'        #A gem that is convenient when dealing with static data such as prefectures

Apply reset CSS

The browser has CSS set by default, so apply it to reset it.

app/views/layouts/application.html


<head>
  <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css">
</head>

Set the element size

If you leave nothing set, when you set padding or border with CSS, it may become an unintended size, so set it so that it has the desired element size.

application.css


* {
  box-sizing: border-box;
}

Set various devise

% rails g devise:install
% rails g devise user
% rails g devise:views

% rails db:migrate

Restrict viewing by unlogged-in users

controller.rb



before_action :authenticate_user!, except: [:index, :show]

Allow columns other than email and password set in devise to be used

app/controllers/application_controller.rb


  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:])
  end

Recommended Posts

List of copy and paste collections that are useful when creating apps with Ruby on Rails
[Ruby on Rails] Implementation of validation that works only when the conditions are met
Ruby on Rails ~ Basics of MVC and Router ~
[Rails] Volume that displays favorites and a list of favorites
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Summary of initial work when creating an app with Rails
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Building an environment for creating apps with Rails and Vue
What happens when remove (int) and remove (Integer) are performed on List <Integer>?
(Ruby on Rails6) Creating a database and displaying it in a view
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Basic knowledge of Ruby on Rails
Copy and paste test with RSpec
Simple slot machine implementation that can be used with copy and paste
[Rails] A collection of tips that are immediately useful for improving performance
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
Ruby on Rails When you don't know the cause of rollback when saving.
[Ruby on Rails] Error that Couldn't find User with ... after logging out
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Creating an inquiry form
[Ruby on Rails] View test with RSpec
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] Model test with RSpec
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Rails 6] Specific method of embedding Font Awesome in link_to [Copy and paste OK]