Skip to content

Instantly share code, notes, and snippets.

@leemcalilly
Created February 11, 2018 18:22
Show Gist options
  • Save leemcalilly/269374e069bbc8887b267c384ae94921 to your computer and use it in GitHub Desktop.
Save leemcalilly/269374e069bbc8887b267c384ae94921 to your computer and use it in GitHub Desktop.
class QuotesController < ApplicationController
before_action :set_artist, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :new, :destroy, :update, :edit ]
before_action :correct_user, only: :destroy
def index
@quotes = Quote.all.paginate(page: params[:page], per_page: 12)
end
def show
end
def new
@quote = Quote.new
end
def create
@quote = current_user.quotes.build(quote_params)
if @quote.save
flash[:success] = "Quote created!"
redirect_to @quote
else
render :new
end
end
def edit
end
def update
if @quote.update(quote_params)
flash[:success] = "Quote updated"
redirect_to @quote
else
render :edit
end
end
def destroy
@quote.destroy
flash[:success] = "Quote deleted"
redirect_back(fallback_location: browse_path)
end
private
def set_artist
@quote = Quote.find(params[:id])
end
def quote_params
params.require(:quote).permit(:content, :source, :topic_id, :speaker_id)
end
def correct_user
@quote = current_user.quotes.find_by(id: params[:id])
redirect_to root_url if @quote.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment