質問編集履歴

3

コメントコントローラの修正

2018/01/21 09:01

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -36,11 +36,19 @@
36
36
 
37
37
 
38
38
 
39
- コントローラーのcreate部分
39
+ ントコントローラー
40
-
40
+
41
- ```
41
+ ```
42
+
42
-
43
+ class CommentsController < ApplicationController
44
+
45
+ before_action :set_comment, only: [:new,:create, :destroy]
46
+
47
+ before_action :require_user_logged_in
48
+
49
+
50
+
43
- def create
51
+ def create
44
52
 
45
53
  @post = Post.find(params[:post_id])
46
54
 
@@ -60,6 +68,8 @@
60
68
 
61
69
  #redirect_to post_comments_path(@post.id)
62
70
 
71
+ #redirect_to :action =>"new"
72
+
63
73
  redirect_back(fallback_location: root_path)
64
74
 
65
75
  else
@@ -70,6 +80,46 @@
70
80
 
71
81
  end
72
82
 
83
+
84
+
85
+ def destroy
86
+
87
+ @comment.destroy
88
+
89
+ flash[:success] = 'コメントを削除しました。'
90
+
91
+ redirect_back(fallback_location: root_path)
92
+
93
+ end
94
+
95
+
96
+
97
+ private
98
+
99
+ # Use callbacks to share common setup or constraints between actions.
100
+
101
+ def set_comment
102
+
103
+ @post = Post.find(params[:post_id])
104
+
105
+ @comment = @post.comments.find(params[:id])
106
+
107
+ end
108
+
109
+
110
+
111
+ # Never trust parameters from the scary internet, only allow the white list through.
112
+
113
+ def comment_params
114
+
115
+ params.require(:comment).permit(:user_id, :post_id, :content)
116
+
117
+ end
118
+
119
+ end
120
+
121
+
122
+
73
123
  ```
74
124
 
75
125
 

2

トップページのcontrollerを追加

2018/01/21 09:01

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -130,6 +130,32 @@
130
130
 
131
131
 
132
132
 
133
+ トップページのcontroller
134
+
135
+ ```
136
+
137
+ class ToppagesController < ApplicationController
138
+
139
+ def index
140
+
141
+ if logged_in?
142
+
143
+ @user = current_user
144
+
145
+ @post = current_user.posts.build # form_for 用
146
+
147
+ @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
148
+
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+
155
+ ```
156
+
157
+
158
+
133
159
  ビュー部分を、```<%= link_to "コメントを投稿", method: :post, :action =>"create", :controller =>"comments", :post_id =>nil, class: 'btn btn-danger btn-sm' %>```や、```<%= form_tag(post_comments_path(@post.id), :action =>"create", :controller =>"comments", :post_id =>nil, method: :post) do %>```としてみたり、コントローラーのコメントアウト部分を一時有効にしてみたり、ググったりしてみましたが、中々解決できませんでした。
134
160
 
135
161
 

1

ルーティングの追加

2018/01/20 04:39

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -74,6 +74,62 @@
74
74
 
75
75
 
76
76
 
77
+ ルーティング
78
+
79
+ ```
80
+
81
+ Rails.application.routes.draw do
82
+
83
+ root to: 'toppages#index'
84
+
85
+
86
+
87
+ get 'login', to: 'sessions#new'
88
+
89
+ post 'login', to: 'sessions#create'
90
+
91
+ delete 'logout', to: 'sessions#destroy'
92
+
93
+
94
+
95
+ resources :users do
96
+
97
+ member do
98
+
99
+ get :followings
100
+
101
+ get :followers
102
+
103
+ end
104
+
105
+ collection do
106
+
107
+ get :search
108
+
109
+ end
110
+
111
+ end
112
+
113
+
114
+
115
+ resources :posts, only: [:create, :destroy, :show] , shallow: true do
116
+
117
+ resources :comments, only: [:create, :destroy]
118
+
119
+ end
120
+
121
+
122
+
123
+ resources :relationships, only: [:create, :destroy]
124
+
125
+ get 'signup', to: 'users#new'
126
+
127
+ end
128
+
129
+ ```
130
+
131
+
132
+
77
133
  ビュー部分を、```<%= link_to "コメントを投稿", method: :post, :action =>"create", :controller =>"comments", :post_id =>nil, class: 'btn btn-danger btn-sm' %>```や、```<%= form_tag(post_comments_path(@post.id), :action =>"create", :controller =>"comments", :post_id =>nil, method: :post) do %>```としてみたり、コントローラーのコメントアウト部分を一時有効にしてみたり、ググったりしてみましたが、中々解決できませんでした。
78
134
 
79
135