72 lines
1.3 KiB
Ruby
72 lines
1.3 KiB
Ruby
class ClipsController < ApplicationController
|
|
allow_unauthenticated_access only: %i[ index show ]
|
|
before_action :set_clip, only: %i[ show edit update destroy ]
|
|
def index
|
|
@clips = Clip.all
|
|
end
|
|
|
|
def show
|
|
end
|
|
|
|
def new
|
|
@clip = Clip.new
|
|
end
|
|
|
|
def create
|
|
@clip = Clip.new(clip_params)
|
|
if @clip.save
|
|
params[:clip][:tag_ids].each do |tag_id|
|
|
unless tag_id.empty?
|
|
tag = Tag.find(tag_id)
|
|
@clip.tags << tag
|
|
end
|
|
end
|
|
|
|
redirect_to @clip
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
if @clip.update(clip_params)
|
|
newtags = []
|
|
params[:clip][:tag_ids].each do |tag_id|
|
|
unless tag_id.empty?
|
|
tag = Tag.find(tag_id)
|
|
newtags.push(tag)
|
|
unless @clip.tags.include? tag
|
|
@clip.tags << tag
|
|
end
|
|
end
|
|
end
|
|
|
|
@clip.tags.each do |tag|
|
|
unless newtags.include? tag
|
|
@clip.tags.delete tag
|
|
end
|
|
end
|
|
|
|
redirect_to @clip
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@clip.destroy
|
|
redirect_to clips_path
|
|
end
|
|
|
|
private
|
|
def set_clip
|
|
@clip = Clip.find(params[:id])
|
|
end
|
|
|
|
def clip_params
|
|
params.expect(clip: [ :title, :video, :category_id ])
|
|
end
|
|
end
|