diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9..ba4f6ac 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -21,6 +21,9 @@ def new def edit respond_to do |format| format.html + format.js do + render template: "comments/edit.js.erb" + end end end @@ -33,6 +36,9 @@ def create if @comment.save format.html { redirect_back fallback_location: root_path, notice: "Comment was successfully created." } format.json { render :show, status: :created, location: @comment } + format.js do + render template: "comments/create.js.erb" + end else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -46,6 +52,9 @@ def update if @comment.update(comment_params) format.html { redirect_to root_url, notice: "Comment was successfully updated." } format.json { render :show, status: :ok, location: @comment } + format.js do + render template: "comments/update.js.erb" + end else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -57,8 +66,11 @@ def update def destroy @comment.destroy respond_to do |format| - format.html { redirect_back fallback_location: root_url, notice: "Comment was successfully destroyed." } - format.json { head :no_content } + #format.html { redirect_back fallback_location: root_url, notice: "Comment was successfully destroyed." } + #format.json { head :no_content } + format.js do + render template: "comments/destroy.js.erb" + end end end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 49affd3..67d45f9 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -27,6 +27,7 @@ def create if @like.save format.html { redirect_back fallback_location: root_url, notice: "Like was successfully created." } format.json { render :show, status: :created, location: @like } + format.js else format.html { render :new, status: :unprocessable_entity } format.json { render json: @like.errors, status: :unprocessable_entity } diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..a4a8165 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -
  • +
  • <%= image_tag comment.author.avatar_image, class: "rounded-circle mr-2", width: 36 %>
    @@ -9,11 +9,14 @@
    <% if current_user == comment.author %> - <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted" do %> + <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted", remote: true do %> <% end %> - <%= link_to comment, method: :delete, class: "btn btn-link btn-sm text-muted" do %> + <%= link_to comment, + method: :delete, + class: "btn btn-link btn-sm text-muted", + remote: true do %> <% end %> <% end %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index f289224..bc2f727 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,5 +1,5 @@ -
  • - <%= form_with(model: comment) do |form| %> +
  • + <%= form_with(model: comment, local: false) do |form| %> <% if comment.errors.any? %>
    - \ No newline at end of file + diff --git a/app/views/comments/edit.js.erb b/app/views/comments/edit.js.erb new file mode 100644 index 0000000..97a3614 --- /dev/null +++ b/app/views/comments/edit.js.erb @@ -0,0 +1,3 @@ +console.log("Editor opened for '<%= @comment.body %>'.") + +$("#<%= dom_id(@comment) %>").replaceWith("<%= j(render "comments/form", comment: @comment) %>"); diff --git a/app/views/comments/update.js.erb b/app/views/comments/update.js.erb new file mode 100644 index 0000000..92bc786 --- /dev/null +++ b/app/views/comments/update.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment.photo) %>_form").replaceWith("<%= j(render @comment) %>"); diff --git a/app/views/likes/_form.html.erb b/app/views/likes/_form.html.erb index eff47d7..70efde7 100644 --- a/app/views/likes/_form.html.erb +++ b/app/views/likes/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: like, class: "d-inline-block") do |form| %> +<%= form_with(model: like, class: "d-inline-block", local: false) do |form| %> <%= form.hidden_field :fan_id %> <%= form.hidden_field :photo_id %> diff --git a/app/views/likes/create.js.erb b/app/views/likes/create.js.erb new file mode 100644 index 0000000..9c709fd --- /dev/null +++ b/app/views/likes/create.js.erb @@ -0,0 +1,3 @@ +console.log("New like.") + +var new_like = $("<%= escape_javascript(render 'likes/create', like: @like) %>");