diff --git a/Gemfile b/Gemfile index 2450b1a..609bb61 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,6 @@ gem "rails", "5.2.1" gem "delayed_job_active_record" gem "dragonfly" gem "http" -gem "jquery-rails" gem "omniauth-github" gem "pg" gem "puma" diff --git a/Gemfile.lock b/Gemfile.lock index 105fabc..082211e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -102,10 +102,6 @@ GEM http_parser.rb (0.6.0) i18n (1.1.0) concurrent-ruby (~> 1.0) - jquery-rails (4.3.3) - rails-dom-testing (>= 1, < 3) - railties (>= 4.2.0) - thor (>= 0.14, < 2.0) jwt (1.5.6) launchy (2.4.3) addressable (~> 2.3) @@ -175,6 +171,8 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) + rails-ujs (0.1.0) + railties (>= 3.1) railties (5.2.1) actionpack (= 5.2.1) activesupport (= 5.2.1) @@ -264,7 +262,6 @@ DEPENDENCIES dotenv-rails dragonfly http - jquery-rails launchy omniauth-github pg @@ -272,6 +269,7 @@ DEPENDENCIES pry puma rails (= 5.2.1) + rails-ujs rspec-rails sass-rails sdoc diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 0dee1df..f673b35 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -7,9 +7,10 @@ class TagsController < ApplicationController image.tags << tag unless image.tags.include?(tag) image.save - respond_to do |format| - format.html { redirect_to([:user, image]) } - format.js { render "index", locals: { image: image, tags: image.tags } } + if request.xhr? + render partial: "tags/tag", locals: { image: image, tag: tag } + else + redirect_to([:user, image]) end end diff --git a/app/javascript/controllers/tag_controller.js b/app/javascript/controllers/tag_controller.js index 35a9d09..fb7d6e8 100644 --- a/app/javascript/controllers/tag_controller.js +++ b/app/javascript/controllers/tag_controller.js @@ -2,15 +2,7 @@ import { Controller } from "stimulus"; import ajaxService from "../services/ajax_service"; export default class extends Controller { - static targets = [ "name" ]; - - connect() { - console.log("Connected to our tags"); - } - - disconnect() { - console.log("Bye bye tag!"); - } + static targets = ["name"]; delete(event) { const imageId = this.element.dataset.imageId; diff --git a/app/javascript/controllers/tags_controller.js b/app/javascript/controllers/tags_controller.js new file mode 100644 index 0000000..197dfa0 --- /dev/null +++ b/app/javascript/controllers/tags_controller.js @@ -0,0 +1,11 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["tag", "tagList"] + + onPostSuccess(event) { + let [data, status, xhr] = event.detail; + this.tagListTarget.innerHTML += xhr.response; + this.tagTarget.value = ""; + } +} diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index a04b5aa..7c49ac0 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -7,9 +7,12 @@ // To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate // layout file, like app/views/layouts/application.html.erb +import Rails from "rails-ujs"; import { Application } from "stimulus"; import { definitionsFromContext } from "stimulus/webpack-helpers"; +Rails.start(); + const application = Application.start(); const context = require.context("controllers", true, /.js$/); application.load(definitionsFromContext(context)); diff --git a/app/views/images/_info.html.erb b/app/views/images/_info.html.erb new file mode 100644 index 0000000..a12b376 --- /dev/null +++ b/app/views/images/_info.html.erb @@ -0,0 +1,20 @@ +
+
+ <%= render "tags/tags", image: image, tags: image.tags %> + +
+ <%= form_with url: user_image_tags_path(image), + method: :post, + html: { + data: { + remote: true, + type: "html", + action: "ajax:success->tags#onPostSuccess" + } + } do %> + <%= text_field_tag :tag, nil, data: { target: "tags.tag" } %> + <%= submit_tag "Add Tag" %> + <% end %> +
+
+
diff --git a/app/views/images/show.html.erb b/app/views/images/show.html.erb index 6080784..fdeab8f 100644 --- a/app/views/images/show.html.erb +++ b/app/views/images/show.html.erb @@ -9,18 +9,5 @@ <%= render "image", image: @image %> -
-
- <%= render "tags/tags", image: @image, tags: @image.tags %> - -
- <%= form_tag user_image_tags_path(@image), - remote: true, - method: :post do %> - <%= text_field_tag :tag %> - <%= submit_tag "Add Tag" %> - <% end %> -
-
-
+ <%= render "info", image: @image %> diff --git a/app/views/tags/_tags.html.erb b/app/views/tags/_tags.html.erb index 9718f7a..14d52a7 100644 --- a/app/views/tags/_tags.html.erb +++ b/app/views/tags/_tags.html.erb @@ -1,4 +1,4 @@ -