creddit/app/controllers/subcreddits_controller.rb
Andrew Tomaka d3dd0ff848 Add the subcreddit MVC
Notes on closing subcreddit:

This doesn't seem great.  Would be slightly better if closed could be referenced
as true or false instead of '0' or '1' in the model.  Testing this is a bit
messy, but Tiemcop makes it a bit better.

Notes on subcreddit validations:

This is still not ideal.  For starters, from
http://guides.rubyonrails.org/active_record_validations.html:

"Note that some databases are configured to perform case-insensitive searches
anyway."

It also seems difficult to validate fields that are set in before_save since
validations occur before callbacks.  Revisit this (perhaps friendly_id wll save
us).
2015-07-13 17:01:12 -04:00

50 lines
936 B
Ruby

class SubcredditsController < ApplicationController
before_filter :set_subcreddit, only: [:show, :edit, :update]
def index
@subcreddits = Subcreddit.all
end
def show
end
def new
@subcreddit = Subcreddit.new
end
def create
@subcreddit = Subcreddit.new(create_subcreddit_params)
@subcreddit.owner = current_user
if @subcreddit.save
redirect_to @subcreddit, notice: 'Subcreddit was created!'
else
render :new
end
end
def edit
end
def update
if @subcreddit.update(update_subcreddit_params)
redirect_to @subcreddit, notice: 'Subcreddit was updated!'
else
render :edit
end
end
private
def create_subcreddit_params
params.require(:subcreddit).permit(:name)
end
def update_subcreddit_params
params.require(:subcreddit).permit(:closed)
end
def set_subcreddit
@subcreddit = Subcreddit.find_by_slug(params[:id])
end
end