Utilizing what I learned at online school I decided to write this as a memorandum. Since it is written by an amateur, I would appreciate it if you could forgive me if there was something different. It's quick, but let's go for the first time.
rails new App name you want to make-d postgresql
❇︎ Basic rails defaults to SQLite3, so you can specify the database with -d.
--Make it feel like returning to the folder with the app name you created Example) cd my_favorite
【Terminal】
rails db:create
--Drop the folder of your own app into VS Code. Then you can edit it with VS Code.
--Write the following in gemfile
【gemfile】
 gem 'devise'
--When the addition is completed, execute the following
【Terminal】
bundle install
【Terminal】
 rails g devise:install(Be careful of forgetting the colon)
❇︎ The reason for rails g devise: install is that devise uses a dedicated command and You need to do this because you need to create a config file
【Terminal】
rails g devise user(I don't need a colon)
【Terminal】
 rails db:migrate
--Write the following in application.html.erb
<% if user_signed_in? %>
 <%= link_to "Log out", destroy_user_session_path, method: :delete %>
 <%= link_to "Top", products_path %>
 <%= link_to "To the posting screen", new_product_path %>
<% else %>
 <%= link_to "Login", new_user_session_path %>
 <%= link_to "sign up", new_user_registration_path %>
<% end %>
--Write the following
【Terminal】
rails g model product
--Once done, edit the migration file item as follows
class CreateProducts < ActiveRecord::Migration[5.2]
  def change
   create_table :products do |t|
      t.string :name
      t.integer :price
      t.integer :user_id
      #So far
      t.timestamps
    end
  end
end
【Terminal】
rails db:migrate
--Edit data in db / seeds.rb
【seeds.rb】
Product.create(name: "Getting Started with Ruby", price: 2500, user_id: 1)
Product.create(name: "Rails basics", price: 2900, user_id: 1)
Product.create(name: "PHP basics", price: 1500, user_id: 1)
【Terminal】
 rails db:seed
--If this does not cause an error, it is successfully registered in the table.
【Terminal】
rails g controller Products
--Edit config / routes.rb as follows
 Rails.application.routes.draw do
 root to: 'products#index'
 resources :products
 end
【products_controller.rb】
 class ProductsController < ApplicationController
 def index
  @products= Product.all
  end
 end
【index.html.erb】
<h1>New post list</h1>
   <% if user_signed_in? %>
   <% @products.each do |product| %>
    <p>Product name:<%= product.name %></p>
    <p>Amount of money:<%= product.price %></p>
   <% else %>
Excuse me, but please log in
   <% end %>
 <% end %>
--When you implement the login function with devise, the login / sign-up screen is automatically generated, but it is applicable in views. The file to be used does not exist. Therefore, you cannot make changes to the login screen as it is. If you want to make changes, you need to use the devise command to generate a view file. --Execute the following command to generate a view file for the login screen.
【Terminal】
rails g devise:views
【products_controller.rb】
 class ProductsController < ApplicationController
  def new
   @product = Product.new
  end
 end
--Once you have done the above, create a new post form in new.html.erb in the users directory.
【new.html.erb】
<h1>New post</h1>
<%= form_with model: @product, local: true do |form| %>
<p>Product name:<%= form.text_field :name, required: true %></p>
<p>Amount of money:<%= form.text_field :age, required: true %></p>
<%= form.submit "Send" %>
<% end %>
【products_controller.rb】
 def create
  Product.create(name: product_params[:name], price: product_params[:price], 
  user_id: current_user.id)
  redirect_to root_path
  end
--Once you can define the create action, the strong parameter is here! Described as follows at the bottom of the products controller
 private
  def product_params
   params.require(:user).permit(:name, :price)
  end
――Explaining this, it says that only (: name,: price) linked to the user model is allowed. Is defining that
【products_controller.rb】
class ProductsController < ApplicationController
  before_action :move_to_index, except: :index ← Add here
  def index
    @products = Product.all
  end
  def new
    @product = Product.new
  end
  def create
    Product.create(name: product_params[:name], price: product_params[:price], user_id: current_user.id)
  end
  def move_to_index
    redirect_to action: :index unless user_signed_in?← Add here
  end
Omitted below
--If you write this before the action, if you are not logged in, you will not be able to go to other actions and will be forced to go to the index action.
【routes.rb】
 Rails.application.routes.draw do
 root to: 'products#index'
 resources :products
 resources :users, only: :show ← Add this
 end
--This time, only show action is used in users_controller, so write as above.
【Terminal】
rails g controller Users
【users_controller.rb】
 class UsersController < ApplicationController
  def show
   @products = Product.where(user_id: current_user.id)
  end
 end
【show.html.erb】
<% @products.each do |product| %>
  <p>Product name:<%= product.name %></p>
  <p>price:<%= product.price %></p>
<% end %>
<%= link_to 'to My page', user_path(current_user.id) %>
【index.html.erb】
<h1>New post list</h1>
   <% if user_signed_in? %>
    <% @products.each do |product| %>
    <p>Product name:<%= product.name %></p>
    <p>Amount of money:<%= product.price %></p>
    <p>Member No.:<%= product.user.id %></p>   
   <% else %>
Excuse me, but please log in
   <% end %>
 <% end %>
――However, if this is left as it is, an error will occur. What to do from here is the process called association To execute. As an image, an image that combines user and product
--Edit product.rb in app / models as follows
【product.rb】
 class Product < ApplicationRecord
  belongs_to :user ← Describe here
 end
――If you explain, who owns the product? If you think about it, you should think that you are a user. Write like this
--Edit user.rb in app / models as follows
【user.rb】
class User < ApplicationRecord 
#Include default devise modules. Others available are:
 #:confirmable, :lockable, :timeoutable, :trackable and :omniauthable
 devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable
 has_many :products ← Describe here
 end
――Explaining, what does user have a lot? If you think that you have a lot of products I'll write it like this
【users_controller.rb】
 class UsersController < ApplicationController
 def show
 @products = Product.where(user_id: current_user.id)
  end
 end
--After change
【users_controller.rb】
def show
 @products = User.find(current_user.id)
end
【show.html.erb】
<% @products.each do |product| %>
  <p>Product name:<%= product.name %></p>
  <p>price:<%= product.price %></p>
<% end %>
--After change
【show.html.erb】
<% @user.products.each do |product| %>(←@user.Change to products)
  <p>Product name:<%= product.name %></p>
  <p>price:<%= product.price %></p>
<% end %>
With this, the implementation of the login function and the rough process of crud have been completed. I wanted to explain the implementation of edit and update actions, the flash function, decoration with bootstrap, etc., but this time I will leave it here. In the future, when creating your own portfolio, when implementing the login function, it is better to implement devise first and then perform crud processing etc., so it may be less likely to get stuck. (I was like that lol) Thank you very much.
Recommended Posts