Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class PhotosController < ApplicationController
before_action :set_photo, only: %i[ show edit update destroy ]

before_action :ensure_current_user_is_owner

def ensure_current_user_is_owner
if current_user != @photo.owner
redirect_back(fallback_location: root_url, alert: "You're not authorized")
end
end

# GET /photos or /photos.json
def index
@photos = Photo.all
Expand Down Expand Up @@ -50,11 +58,20 @@ def update

# DELETE /photos/1 or /photos/1.json
def destroy
@photo.destroy
respond_to do |format|
format.html { redirect_back fallback_location: root_url, notice: "Photo was successfully destroyed." }
format.json { head :no_content }
if current_user == @photo.owner

@photo.destroy

respond_to do |format|
format.html { redirect_back fallback_location: root_url, notice: "Photo was successfully destroyed." }
format.json { head :no_content }
end
else
redirect_back(fallback_location: root_url, alert: "nice try, sucka")
end



end

private
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
devise_for :users

resources :comments
resources :follow_requests
resources :likes
resources :follow_requests, except: [:index, :show, :new, :edit]
resources :likes, only: [:create, :destroy]
resources :photos

get ":username" => "users#show", as: :user
Expand All @@ -14,4 +14,4 @@
get ":username/discover" => "users#discover", as: :discover
get ":username/followers" => "users#followers", as: :followers
get ":username/following" => "users#following", as: :following
end
end