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

質問編集履歴

2

しんてんがあったのでついきしました

2021/01/29 02:06

投稿

kawasaki4563
kawasaki4563

スコア32

title CHANGED
File without changes
body CHANGED
@@ -151,4 +151,13 @@
151
151
  intel mac big sur
152
152
  rails 5.2.2
153
153
  ruby 2.6.5
154
- docker 20.10.0
154
+ docker 20.10.0
155
+
156
+ #追記
157
+ posts/index.html.erbにの以下のコード
158
+ `<% @posts.comment.each do |comment|%>`
159
+ を載せて見た後に、postのほうのmodelに以下のコードを追加したら
160
+ `belongs_to :comments`
161
+ エラーになってしまいました。
162
+ スクショを貼っておきます
163
+ ![イメージ説明](e324de9f96b1c148df28b43172970c9c.png)

1

controllerを追記しました。それと、dockerを使っているので、投稿したときのログも載せておきます

2021/01/29 02:06

投稿

kawasaki4563
kawasaki4563

スコア32

title CHANGED
File without changes
body CHANGED
@@ -62,6 +62,89 @@
62
62
  <% end %>
63
63
  </p>
64
64
  ```
65
+ 以下、テキストを投稿機能を作るときに作ったcontroller
66
+ ```
67
+ class PostsController < ApplicationController
68
+ def index
69
+ @posts = Post.all
70
+ @comment = Comment.all
71
+
72
+ end
73
+
74
+ def new
75
+ @post = Post.new
76
+ end
77
+
78
+ def create
79
+ @post = Post.new(post_params)
80
+ @post.save
81
+ redirect_to action: 'index'
82
+ end
83
+
84
+ private
85
+ def post_params
86
+ params.require(:post).permit(:title, :body)
87
+ end
88
+ end
89
+ ```
90
+
91
+ いか、画像投稿機能を作るときに作ったcontroller
92
+ ```
93
+ class CommentsController < ApplicationController
94
+ def idndex
95
+ @comment = Comment.all
96
+ end
97
+
98
+ def new
99
+ @comment = Comment.new
100
+ end
101
+
102
+ def create
103
+ @comment = Comment.create params.require(:comment).permit(:content, :image) # POINT
104
+ @comment.save
105
+ redirect_to @comment
106
+ end
107
+
108
+ def show
109
+ @comment = Comment.find(params[:id])
110
+ end
111
+
112
+ def edit
113
+ @comment = Comment.find(params[:id])
114
+ end
115
+
116
+ def update
117
+ @comment = Comment.find(params[:id])
118
+ @comment.update params.require(:comment).permit(:content, :image) # POINT
119
+ redirect_to @comment
120
+ end
121
+ end
122
+ ```
123
+ ログ
124
+ ```
125
+ Processing by PostsController#create as HTML
126
+ web_1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"r53Nupv5tPGPeSQOiMq/tktaETd2Vf0sL712vnlqkoc5gHDvdpiAemcaPPJL4OY55Z/YmHw85xJm6kAP4avMKw==", "post"=>{"title"=>"ddddddd", "body"=>"xxxxxxxxxxxxxx"}, "images"=>["IMG_20201122_180927.jpg"]}
127
+ web_1 | (0.4ms) BEGIN
128
+ web_1 | ↳ app/controllers/posts_controller.rb:14
129
+ web_1 | Post Create (0.6ms) INSERT INTO `posts` (`title`, `body`, `created_at`, `updated_at`) VALUES ('ddddddd', 'xxxxxxxxxxxxxx', '2021-01-28 08:31:27', '2021-01-28 08:31:27')
130
+ web_1 | ↳ app/controllers/posts_controller.rb:14
131
+ web_1 | (2.1ms) COMMIT
132
+ web_1 | ↳ app/controllers/posts_controller.rb:14
133
+ web_1 | Redirected to http://localhost:3000/posts
134
+ web_1 | Completed 302 Found in 15ms (ActiveRecord: 3.1ms)
135
+ web_1 |
136
+ web_1 |
137
+ web_1 | Started GET "/posts" for 172.18.0.1 at 2021-01-28 08:31:27 +0000
138
+ web_1 | Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
139
+ web_1 | Processing by PostsController#index as HTML
140
+ web_1 | Rendering posts/index.html.erb within layouts/application
141
+ web_1 | Comment Load (0.5ms) SELECT `comments`.* FROM `comments`
142
+ web_1 | ↳ app/views/posts/index.html.erb:10
143
+ web_1 | Rendered posts/index.html.erb within layouts/application (3.1ms)
144
+ web_1 | Completed 200 OK in 190ms (Views: 161.2ms | ActiveRecord: 0.5ms)
145
+ web_1 |
146
+ web_1
147
+ ```
65
148
  #試したこと
66
149
  <%= yield %>を投稿詳細ページの方にも追加しました
67
150
  ##動作環境