質問するログイン新規登録

質問編集履歴

1

変更点、内容が全て保存出来ていなかった。

2021/01/19 14:09

投稿

YukiTani
YukiTani

スコア19

title CHANGED
File without changes
body CHANGED
@@ -78,54 +78,6 @@
78
78
  ```
79
79
 
80
80
  ```rails
81
- Commentコントローラー
82
-
83
- class CommentsController < ApplicationController
84
- def create
85
- @comment = Comment.new(comment_params)
86
- if @comment.save
87
- redirect_to post_path(@comment.post)
88
- else
89
- @post = @comment.post
90
- @comments = @post.comments
91
- render "posts/show"
92
- end
93
- end
94
-
95
- private
96
- def comment_params
97
- params.require(:comment).permit(:text).merge(user_id: current_user.id, post_id: params[:post_id])
98
- end
99
- end
100
- ```
101
-
102
- ```rails
103
- ビューファイル
104
- <div class="type__comments">
105
- <% if user_signed_in? %>
106
- <%= form_with model: [@post, @comment], local: true do |f|%>
107
- <div class="field">
108
- <%= f.label :text, "コメント" %><br />
109
- <%= f.text_field :text %>
110
- </div>
111
- <div class="actions">
112
- <%= f.submit "送信する", class: :form__btn %>
113
- </div>
114
- <% end %>
115
- <% end %>
116
- <ul class="comments_lists">
117
- <% @comments.each do |comment| %>
118
- <% if @comments %>
119
- <li class="comments_list">
120
- <%= comment.text %>
121
- <%= link_to comment.user.name, user_path(comment.user_id), class: :comment_user %>
122
- </li>
123
- <% end %>
124
- <% end %>
125
- </ul>
126
- </div>
127
- ```
128
- ```rails
129
81
  ルーティング
130
82
 
131
83
  Rails.application.routes.draw do
@@ -138,34 +90,18 @@
138
90
  end
139
91
  ```
140
92
  ```rails
141
- commentモデル
142
- class Comment < ApplicationRecord
93
+ class Prototype < ApplicationRecord
143
- validates :text, presence: true
144
- validates :user, presence: true
145
- validates :text, presence: true
146
-
147
- belongs_to :post
148
94
  belongs_to :user
149
- end
95
+ has_one_attached :image
96
+ has_many :comments, dependent: :destroy ←ここが抜けていました。 dependent: :destroyも一緒に追記
150
97
 
151
- postモデル
98
+ validates :title, presence: true
152
- class Post < ApplicationRecord
99
+ validates :catch_copy, presence: true
100
+ validates :concept, presence: true
153
- validates :image, presence: true
101
+ validates :image, presence: true
154
-
155
- belongs_to :user
156
- has_one_attached :image
157
-
158
102
  end
159
-
160
103
  ```
161
104
 
162
105
 
163
106
  ### 試したこと
164
- 発生しているエラーは「NoMethodError」で、undefined method`comments' というメソッドが定義されていないといことなので、@commentsという変数が定義されていないのでエラーということはわかりましたが、before_action_set_postで @post = Post.find(params[:id])をわたせるようになっています。原因追求に困っております。
165
-
166
- どうぞ宜しくお願いします。
167
-
168
-
169
- ### 補足情報(FW/ツールのバージョンなど)
170
-
171
- ここにより詳細な情報を記載してください。
107
+ 発生しているエラーは「NoMethodError」で、undefined method`comments' というメソッドが定義されていないといことなので、@commentsという変数が定義されていないのでエラーということはわかりましたが、before_action_set_postで @post = Post.find(params[:id])をわたせるようになっています。原因追求に困っております。