-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
139 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
# app/controllers/application_controller.rb | ||
class ApplicationController < ActionController::Base | ||
before_action :require_login | ||
helper_method :current_user | ||
|
||
private | ||
|
||
def current_user | ||
@current_user ||= User.find_by(id: session[:user_id]) | ||
end | ||
|
||
def require_login | ||
unless current_user | ||
redirect_to login_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,49 @@ | ||
class CommentsController < ApplicationController | ||
before_action :set_comment, only: [:edit, :update, :destroy] | ||
before_action :check_ownership, only: [:edit, :update, :destroy] | ||
|
||
def create | ||
@post = Post.find(params[:post_id]) | ||
@comment = @post.comments.build(comment_params) | ||
@comment.user = current_user | ||
@comment.user = current_user if current_user # Associate with user if authenticated | ||
|
||
if @comment.save | ||
redirect_to post_path(@post), notice: 'Comment was successfully added.' | ||
else | ||
redirect_to post_path(@post), alert: @comment.errors.full_messages.join(', ') | ||
end | ||
end | ||
|
||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @comment.update(comment_params) | ||
redirect_to post_path(@comment.post), notice: 'Comment was successfully updated.' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
post = @comment.post | ||
@comment.destroy | ||
redirect_to post_path(post), notice: 'Comment was successfully deleted.' | ||
end | ||
|
||
private | ||
|
||
|
||
def set_comment | ||
@comment = Comment.find(params[:id]) | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:content) | ||
end | ||
|
||
def check_ownership | ||
unless @comment.user == current_user | ||
redirect_to root_path, alert: 'You can only modify your own comments.' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<div class="comment-form"> | ||
<h1>Edit Comment</h1> | ||
|
||
<%= form_for [@comment.post, @comment] do |f| %> | ||
<% if @comment.errors.any? %> | ||
<div class="alert alert-danger"> | ||
<ul> | ||
<% @comment.errors.full_messages.each do |msg| %> | ||
<li><%= msg %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="form-group"> | ||
<%= f.text_area :content, class: 'form-control', rows: 3 %> | ||
</div> | ||
|
||
<div class="form-actions"> | ||
<%= f.submit "Update Comment", class: 'btn btn-primary' %> | ||
<%= link_to 'Back', post_path(@comment.post), class: 'btn btn-secondary' %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
Rails.application.routes.draw do | ||
root 'posts#index' | ||
|
||
get '/login', to: 'sessions#new' | ||
post '/login', to: 'sessions#create' | ||
get '/logout', to: 'sessions#destroy' | ||
|
||
resources :posts do | ||
resources :comments, only: [:create] | ||
resources :comments, only: [:create, :edit, :update, :destroy] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AllowNullUserInPosts < ActiveRecord::Migration[7.1] | ||
def change | ||
change_column_null :posts, :user_id, true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AllowNullUserInComments < ActiveRecord::Migration[7.1] | ||
def change | ||
change_column_null :comments, :user_id, true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.