Last active
October 31, 2023 13:59
-
-
Save akapitula/41aa66222ba934acb373c7b3bad1f563 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# GIVEN TWO CONTROLLERS, CatsController and DogsController: | |
class CatsController < ApplicationController | |
before_action :set_cat, only: [:show, :edit, :update, :destroy] | |
def new | |
@cat = Cat.new | |
end | |
def create | |
@cat = Cat.new(cat_params) | |
if @cat.save | |
flash[:notice] = I18n.t('cat.create.success') | |
redirect_to cat_path(@cat) | |
else | |
flash[:alert] = @cat.errors.full_messages | |
render :new | |
end | |
end | |
def index | |
@cats = Cat.all | |
end | |
def show; end | |
def edit; end | |
def update | |
@cat.attributes = cat_params | |
if @cat.save | |
flash[:notice] = I18n.t('cat.update.success') | |
redirect_to cat_path(@cat) | |
else | |
flash[:alert] = @cat.errors.full_messages | |
render :edit | |
end | |
end | |
def destroy | |
@cat.destroy! | |
flash[:notice] = I18n.t('cat.destroy.success') | |
rescue ActiveRecord::RecordNotDestroyed => error | |
flash[:alert] = I18n.t('cat.destroy.fail', error.record.errors) | |
render :show | |
end | |
private | |
def set_cat | |
@cat = Cat.find(params[:id]) | |
end | |
def cat_params | |
params.require(:cat).permit(:race, :color, :gender, :age) | |
end | |
end | |
class DogsController < ApplicationController | |
before_action :set_dog, only: [:show, :edit, :update, :destroy] | |
def new | |
@dog = Dog.new | |
end | |
def create | |
@dog = Dog.new(dog_params) | |
if @dog.save | |
flash[:notice] = I18n.t('dog.create.success') | |
redirect_to dog_path(@dog) | |
else | |
flash[:alert] = @dog.errors.full_messages | |
render :new | |
end | |
end | |
def index | |
@dogs = Dog.all | |
end | |
def show; end | |
def edit; end | |
def update | |
@dog.attributes = dog_params | |
if @dog.save | |
flash[:notice] = I18n.t('dog.update.success') | |
redirect_to dog_path(@dog) | |
else | |
flash[:alert] = @dog.errors.full_messages | |
render :edit | |
end | |
end | |
def destroy | |
@dog.destroy! | |
flash[:notice] = I18n.t('dog.destroy.success') | |
rescue ActiveRecord::RecordNotDestroyed => error | |
flash[:alert] = I18n.t('dog.destroy.fail', error.record.errors) | |
render :show | |
end | |
private | |
def set_dog | |
@dog = Dog.find(params[:id]) | |
end | |
def dog_params | |
params.require(:dog).permit(:race, :color, :gender, :age) | |
end | |
end | |
# EXERCISE: Both controllers are exactly the same. Unify them so that we can do: | |
class CatsController < ApplicationController | |
include GenericControllerConcern | |
resource_name :cat | |
end | |
class DogsController < ApplicationController | |
include GenericControllerConcern | |
resource_name :dog | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment