thirteen-tien-len/app/controllers/profiles_controller.rb

82 lines
2.3 KiB
Ruby

class ProfilesController < ApplicationController
before_action :set_profile, only: %i[ show edit update destroy ]
# GET /profiles or /profiles.json
def index
@profiles = policy_scope Profile.all
authorize @profiles
end
# GET /profiles/1 or /profiles/1.json
def show
authorize @profile
@followable = @profile
if @profile.user == current_user
@micropost = @profile.microposts.build
else
@micropost = current_user.microposts.build if current_user.signed_in?
end
@microposts = @profile.microposts.paginate(page: params[:page])
end
# GET /profiles/new
def new
authorize @profile = current_user.profiles.new
end
# GET /profiles/1/edit
def edit
authorize @profile
end
# POST /profiles or /profiles.json
def create
authorize @profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to profile_url(@profile), notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
authorize @profile
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to profile_url(@profile), notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :summary, :about)
end
end