65 lines
1.3 KiB
Ruby
65 lines
1.3 KiB
Ruby
|
|
class ProfilesController < ApplicationController
|
||
|
|
allow_unauthenticated_access only: %i[ index show ]
|
||
|
|
before_action :authenticate_user!, only: [:new, :create, :edit, :update]
|
||
|
|
before_action :set_profile, only: [:edit, :update, :update]
|
||
|
|
|
||
|
|
def index
|
||
|
|
@profiles = Profile.all
|
||
|
|
end
|
||
|
|
|
||
|
|
def new
|
||
|
|
@profile = Profile.new
|
||
|
|
end
|
||
|
|
|
||
|
|
def create
|
||
|
|
@profile = Profile.create(profile_params)
|
||
|
|
if @profile.save
|
||
|
|
|
||
|
|
redirect_to @profile, notice: 'Profile was successfully created.'
|
||
|
|
else
|
||
|
|
|
||
|
|
render :new
|
||
|
|
end
|
||
|
|
end
|
||
|
|
def edit
|
||
|
|
|
||
|
|
end
|
||
|
|
|
||
|
|
def update
|
||
|
|
if @profile.update(profile_params)
|
||
|
|
redirect_to @profile, notice: 'Profile was successfully updated!'
|
||
|
|
else
|
||
|
|
render :edit
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def show
|
||
|
|
@profile = Profile.find_by(id: params[:id])
|
||
|
|
|
||
|
|
if @profile.nil?
|
||
|
|
flash[:alert] = "Profile not found"
|
||
|
|
redirect_to root_path
|
||
|
|
end
|
||
|
|
end
|
||
|
|
def destroy
|
||
|
|
@profile = Profile.find(params[:id])
|
||
|
|
@profile.destroy
|
||
|
|
end
|
||
|
|
|
||
|
|
private
|
||
|
|
|
||
|
|
def set_profile
|
||
|
|
@profile = Profile.find_by(id: params[:id]) # Find profile by ID passed in the URL
|
||
|
|
if @profile.nil?
|
||
|
|
redirect_to profiles_path, alert: 'Profile not found.'
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def profile_params
|
||
|
|
params.require(:profile).permit(:bio, :name, :avatar_image, :age, :location, :website, :pronouns)
|
||
|
|
end
|
||
|
|
|
||
|
|
end
|
||
|
|
|
||
|
|
|