Created
February 11, 2018 18:22
-
-
Save leemcalilly/269374e069bbc8887b267c384ae94921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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