File tree Expand file tree Collapse file tree 6 files changed +79
-5
lines changed Expand file tree Collapse file tree 6 files changed +79
-5
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ def index
6
6
7
7
def show
8
8
@article = Article . find ( params [ :id ] )
9
- @comments = @article . comments . order ( created_at : :desc ) . limit ( 20 )
9
+ @comments = @article . comments . order ( created_at : :desc )
10
10
@comment = Comment . new
11
11
end
12
12
Original file line number Diff line number Diff line change 1
1
class CommentsController < ApplicationController
2
- before_action :authorized , only : [ :create ]
2
+ before_action :authorized
3
3
4
4
def create
5
5
@article = Article . find ( params [ :article_id ] )
@@ -9,6 +9,7 @@ def create
9
9
if @comment . save
10
10
redirect_to @article
11
11
else
12
+ @comments = @article . comments . order ( created_at : :desc )
12
13
render "articles/show" , status : :unprocessable_entity
13
14
end
14
15
end
Original file line number Diff line number Diff line change 1
1
Rails . application . routes . draw do
2
2
root "articles#index"
3
3
resources :articles do
4
- resources :comments , only : [ :create , :index ]
4
+ resources :comments , only : [ :create ]
5
5
end
6
- resources :users
7
- resources :articles
8
6
resources :users , only : [ :create , :show ]
9
7
get "/signup" , to : "users#new"
10
8
get "/login" , to : "sessions#new"
Original file line number Diff line number Diff line change 10
10
# Clear existing records
11
11
User . destroy_all
12
12
Article . destroy_all
13
+ Comment . destroy_all
13
14
14
15
# Create users
15
16
users = [
37
38
user : users . sample
38
39
)
39
40
end
41
+
42
+ articles = Article . all
43
+
44
+ # Create comments
45
+ 90 . times do |i |
46
+ Comment . create! (
47
+ text : "Sample Comment #{ i + 1 } " ,
48
+ article : articles . sample ,
49
+ user : users . sample
50
+ )
51
+ end
Original file line number Diff line number Diff line change
1
+ require "test_helper"
2
+
3
+ class CommentsControllerTest < ActionDispatch ::IntegrationTest
4
+ def setup
5
+ @user = create ( :user )
6
+ @article = create ( :article , user : @user )
7
+ @comment_params = { text : "This is a test comment" }
8
+ end
9
+ test "should not create comment when not logged in" do
10
+ assert_no_difference "Comment.count" do
11
+ post article_comments_path ( @article ) , params : { comment : @comment_params }
12
+ end
13
+ assert_redirected_to login_path
14
+ end
15
+
16
+ describe "when logged in" do
17
+ before do
18
+ @user = create ( :user ) # There are errors if I don't add this line. didn't need this in articles_controller_test
19
+ log_in_as ( @user )
20
+ end
21
+
22
+ it "should create comment when logged in" do
23
+ assert_difference ( "Comment.count" , 1 ) do
24
+ post article_comments_path ( @article ) , params : { comment : @comment_params }
25
+ end
26
+ assert_redirected_to @article
27
+ end
28
+ it "should not create comment with invalid params" do
29
+ assert_no_difference "Comment.count" do
30
+ post article_comments_path ( @article ) , params : { comment : { text : "" } }
31
+ end
32
+ assert_response :unprocessable_entity
33
+ end
34
+ end
35
+ end
Original file line number Diff line number Diff line change
1
+ require "test_helper"
2
+
3
+ class CommentTest < ActiveSupport ::TestCase
4
+ def setup
5
+ @user = create ( :user )
6
+ @article = create ( :article , user : @user )
7
+ @comment = Comment . new ( text : "This is a test comment" , article : @article , user : @user )
8
+ end
9
+
10
+ test "should be valid" do
11
+ assert @comment . valid?
12
+ end
13
+
14
+ test "text should be present" do
15
+ @comment . text = " "
16
+ assert_not @comment . valid?
17
+ end
18
+
19
+ test "should belong to an article" do
20
+ @comment . article = nil
21
+ assert_not @comment . valid?
22
+ end
23
+
24
+ test "should belong to a user" do
25
+ @comment . user = nil
26
+ assert_not @comment . valid?
27
+ end
28
+ end
You can’t perform that action at this time.
0 commit comments