Gemfile
#Omission
gem 'devise'
Terminal
#Start the server
% rails s
Terminal
#Create devise config file
% rails g devise:install
Terminal
#Create User model with devise command
% rails g devise user
Terminal
#Perform migration
% rails db:migrate
Terminal
# 「ctrl +Quit the local server with "C"
#Start the local server again
% rails s
app/controllers/tweets_controller.rb
class TweetsController < ApplicationController
before_action :set_tweet, only: [:edit, :show]
before_action :move_to_index, except: [:index, :show]
def index
@tweets = Tweet.all
end
def new
@tweet = Tweet.new
end
def create
Tweet.create(tweet_params)
end
def destroy
tweet = Tweet.find(params[:id])
tweet.destroy
end
def edit
end
def update
tweet = Tweet.find(params[:id])
tweet.update(tweet_params)
end
def show
end
private
def tweet_params
params.require(:tweet).permit(:name, :image, :text)
end
def set_tweet
@tweet = Tweet.find(params[:id])
end
def move_to_index
unless user_signed_in?
redirect_to action: :index
end
end
end
Terminal
rails g devise:views
Terminal
#Make sure the directory is pictweet
% pwd
#Create a migration file that adds a nickname column of type string to the users table
% rails g migration AddNicknameToUsers nickname:string
#Execute the created migration
% rails db:migrate
Terminal
# 「ctrl +Quit the local server with "C"
#Start the local server again
% rails s
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
end
end
Recommended Posts