diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9..17b46af 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,6 +1,5 @@ class CommentsController < ApplicationController before_action :set_comment, only: %i[ show edit update destroy ] - before_action :ensure_current_user_is_owner, only: [:destroy, :update, :edit] # GET /comments or /comments.json @@ -21,6 +20,7 @@ def new def edit respond_to do |format| format.html + format.js end end @@ -33,9 +33,13 @@ 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 } + format.js { render js: @comment.errors, status: :unprocessable_entity } end end end @@ -46,9 +50,13 @@ 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 } + format.js { render js: @comment.errors, status: :unprocessable_entity } end end end @@ -59,23 +67,27 @@ def destroy respond_to do |format| 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 private - # Use callbacks to share common setup or constraints between actions. - def set_comment - @comment = Comment.find(params[:id]) - end - def ensure_current_user_is_owner - if current_user != @comment.author - redirect_back fallback_location: root_url, alert: "You're not authorized for that." - end - end + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end - # Only allow a list of trusted parameters through. - def comment_params - params.require(:comment).permit(:author_id, :photo_id, :body) + def ensure_current_user_is_owner + if current_user != @comment.author + redirect_back fallback_location: root_url, alert: "You're not authorized for that." end + end + + # Only allow a list of trusted parameters through. + def comment_params + params.require(:comment).permit(:author_id, :photo_id, :body) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 6c3e331..139b23f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,11 +35,11 @@ class User < ApplicationRecord has_many :comments, foreign_key: :author_id, dependent: :destroy has_many :sent_follow_requests, foreign_key: :sender_id, class_name: "FollowRequest", dependent: :destroy - + has_many :accepted_sent_follow_requests, -> { accepted }, foreign_key: :sender_id, class_name: "FollowRequest" - + has_many :received_follow_requests, foreign_key: :recipient_id, class_name: "FollowRequest", dependent: :destroy - + has_many :accepted_received_follow_requests, -> { accepted }, foreign_key: :recipient_id, class_name: "FollowRequest" has_many :pending_received_follow_requests, -> { pending }, foreign_key: :recipient_id, class_name: "FollowRequest" @@ -51,7 +51,7 @@ class User < ApplicationRecord has_many :liked_photos, through: :likes, source: :photo has_many :leaders, through: :accepted_sent_follow_requests, source: :recipient - + has_many :followers, through: :accepted_received_follow_requests, source: :sender has_many :pending, through: :pending_received_follow_requests, source: :sender @@ -63,9 +63,9 @@ class User < ApplicationRecord validates :username, presence: true, uniqueness: true, - format: { + format: { with: /\A[\w_\.]+\z/i, - message: "can only contain letters, numbers, periods, and underscores" + message: "can only contain letters, numbers, periods, and underscores", } validates :website, url: { allow_blank: true } @@ -78,9 +78,8 @@ class User < ApplicationRecord def ensure_website_has_scheme if website.present? && - !website.starts_with?("http://") && - !website.starts_with?("https://") - + !website.starts_with?("http://") && + !website.starts_with?("https://") self.website = "http://" + self.website end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..d1063a4 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -
<%= comment.body %>
<%= photo.caption %>