here come the tags
This commit is contained in:
parent
c8732b6332
commit
29368bfdb9
16 changed files with 155 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
2
app/helpers/tag_to_clips_helper.rb
Normal file
2
app/helpers/tag_to_clips_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module TagToClipsHelper
|
||||
end
|
||||
|
|
@ -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
4
app/models/tag.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class Tag < ApplicationRecord
|
||||
has_many :tag_to_clips
|
||||
has_many :clips, through: :tag_to_clips
|
||||
end
|
||||
4
app/models/tag_to_clip.rb
Normal file
4
app/models/tag_to_clip.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class TagToClip < ApplicationRecord
|
||||
belongs_to :clip
|
||||
belongs_to :tag
|
||||
end
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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 %>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue