-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adição da funcionalidade de comentarios nos artigos
- Loading branch information
Showing
13 changed files
with
116 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class CommentsController < ApplicationController | ||
before_action :set_article | ||
|
||
def create | ||
@comment = @article.comments.new(comment_params) | ||
@comment.user = current_user | ||
|
||
if @comment.save | ||
redirect_to @article, notice: 'Comentário criado com sucesso.' | ||
else | ||
redirect_to @article, alert: 'Erro ao criar comentário.' | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_article | ||
@article = Article.find(params[:article_id]) | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:content) | ||
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,2 @@ | ||
module CommentsHelper | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :article | ||
belongs_to :user | ||
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,14 +1,35 @@ | ||
<div class="mx-auto md:w-2/3 w-full bg-gray-900 text-white p-6 rounded-lg shadow-lg"> | ||
|
||
|
||
<!-- Renderização do artigo --> | ||
<%= render @article %> | ||
|
||
<div class="mt-4 md:flex gap-4"> | ||
<div class="mt-4 md:flex gap-4"> | ||
<%= link_to "Voltar aos artigos", articles_path, class: "mb-2 rounded-lg py-2 px-4 bg-gray-700 text-white inline-block font-medium hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-gray-500" %> | ||
<% if @article.user == current_user %> | ||
<%= link_to "Edite este artigo", edit_article_path(@article), class: "mb-2 rounded-lg py-2 px-4 bg-blue-600 text-white inline-block font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500" %> | ||
<%= button_to "Deletar artigo", @article, method: :delete, data: { confirm: "Are you sure you want to delete this article?" }, class: "rounded-lg py-2 px-4 bg-red-600 text-white inline-block font-medium hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500" %> | ||
<% end %> | ||
</div> | ||
</div> | ||
|
||
<!-- Seção de comentários --> | ||
<div class="mt-8"> | ||
<h2 class="text-2xl font-bold mb-4">Comentários</h2> | ||
|
||
<!-- Formulário de comentário --> | ||
<%= form_with(model: [ @article, Comment.new ], local: true) do |form| %> | ||
<div class="mb-4"> | ||
<%= form.text_area :content, placeholder: "Escreva seu comentário...", class: "w-full p-4 bg-gray-700 text-white rounded-lg focus:outline-none focus:ring-2 focus:ring-lime-500" %> | ||
</div> | ||
<%= form.submit "Comentar", class: "bg-lime-600 text-white py-2 px-4 rounded-lg hover:bg-lime-700 focus:outline-none focus:ring-2 focus:ring-lime-500" %> | ||
<% end %> | ||
|
||
<!-- Lista de comentários --> | ||
<div class="mt-6"> | ||
<% @article.comments.each do |comment| %> | ||
<div class="bg-gray-800 p-4 rounded-lg mb-4"> | ||
<p class="text-sm text-gray-400">Comentado por: <%= comment.user.name %></p> | ||
<p class="mt-2 text-white"><%= comment.content %></p> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateComments < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :comments do |t| | ||
t.references :article, null: false, foreign_key: true | ||
t.references :user, null: false, foreign_key: true | ||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
require "test_helper" | ||
|
||
class CommentsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert 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,11 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
article: one | ||
user: one | ||
content: MyText | ||
|
||
two: | ||
article: two | ||
user: two | ||
content: MyText |
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,7 @@ | ||
require "test_helper" | ||
|
||
class CommentTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |