1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Delete tags using Stimulus

This commit is contained in:
Daniel Barber 2018-09-11 22:53:38 -04:00
parent d71f081d7b
commit f5bc989048
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 13 additions and 16 deletions

View File

@ -4,13 +4,12 @@ class TagsController < ApplicationController
def create
image = @current_user.images.find(params[:image_id])
tag = params[:tag]
image.tags << tag unless image.tags.include?(tag)
image.save
if request.xhr?
if !image.tags.include?(tag)
image.tags << tag
image.save
render partial: "tags/tag", locals: { image: image, tag: tag }
else
redirect_to([:user, image])
head 200, content_type: "text/html"
end
end
@ -20,9 +19,6 @@ class TagsController < ApplicationController
image.tags.delete(tag)
image.save
respond_to do |format|
format.html { redirect_to([:user, image]) }
format.js { render "index", locals: { image: image, tags: image.tags } }
end
render json: { status: :ok }
end
end

View File

@ -1,8 +1,9 @@
<li class="image-tag" id="tag-<%= tag %>">
<%= tag %>
<%= link_to "&times;".html_safe,
user_image_tag_path(image, tag),
method: :delete,
remote: true,
class: "delete-tag" %>
<li class="image-tag"
id="tag-<%= tag %>"
data-controller="tag">
<span data-target="tag.name"><%= tag %></span>
<%= link_to "&times;".html_safe,
user_image_tag_path(image, tag),
data: { action: "click->tag#delete" },
class: "delete-tag" %>
</li>