here come the tags

This commit is contained in:
Roscoe 2025-09-10 03:46:23 +01:00
commit 29368bfdb9
Signed by: RoscoeDaWah
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
16 changed files with 155 additions and 2 deletions

View file

@ -15,6 +15,13 @@ class ClipsController < ApplicationController
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
@ -24,8 +31,25 @@ class ClipsController < ApplicationController
def edit
end
def update
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

View file

@ -0,0 +1,2 @@
module TagToClipsHelper
end

View file

@ -1,5 +1,7 @@
class Clip < ApplicationRecord
has_one_attached :video
belongs_to :category
has_many :tag_to_clips
has_many :tags, through: :tag_to_clips
validates :title, presence: true
end

4
app/models/tag.rb Normal file
View file

@ -0,0 +1,4 @@
class Tag < ApplicationRecord
has_many :tag_to_clips
has_many :clips, through: :tag_to_clips
end

View file

@ -0,0 +1,4 @@
class TagToClip < ApplicationRecord
belongs_to :clip
belongs_to :tag
end

View file

@ -4,6 +4,7 @@
<%= form.text_field :title %>
<%= form.file_field :video, :accept => 'video/quicktime,video/mp4' %>
<%= form.select :category_id, Category.all.collect{ |t| [ t.name, t.id ] }%>
<%= collection_select(:clip, :tag_ids, Tag.all, :id, :name, {}, { :multiple => true } )%>
</div>
<div>

View file

@ -2,6 +2,7 @@
<h1><%= @clip.title %></h1>
<% end %>
<b>Category:</b> <%= link_to @clip.category.name, @clip.category %><br>
<b>Tags:</b> <%= @clip.tags.map(&:name).join(', ') %><br>
<% if @clip.video.attached? %>
<%= video_tag @clip.video, :controls => true%>
<% end %>