CRUD and Rails

Rails.application.routes.draw do
resources :users
end
Rails.application.routes.draw do
get 'users/index'
get 'users/show'
get 'users/new'
#get 'users/edit' <- commented out, will not use
end
rails db:migrate
def index
@users = User.all
end
def show
@user = User.find(params[:id])
def create
@user = User.new(user_params)
redirect_to user_path(@user)
end
class User < ApplicationRecord
has_secure_password
has_many :posts
has_many :comments
has_many :communities, through: :posts

validates :name, presence: true
validates :name, uniqueness: true
validates :email, presence: true
validates :email, uniqueness: true
validates :password, presence: true
validates :password, length: { minimum: 6 }
end
def create
@user = User.new(user_params)
if @user.valid?
@user.save
session[:user_id] = @user.id
redirect_to user_path(@user)
else
render :new
end
end
def update
@user = User.find(params[:id])
@user.update(user_params)
redirect_to user_path(@user)
end
def destroy
@user = User.find(params[:id])
@user.destroy
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store