質問編集履歴

3

モデルのコードを挿入し直しました。

2019/09/03 11:57

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -271,3 +271,33 @@
271
271
  投稿に失敗した時のpumaの動きが、こちらになります。
272
272
 
273
273
  ![イメージ説明](e322c320f32a5ae2e783b1ad7d6fe2b6.png)
274
+
275
+
276
+
277
+ productモデルが、こちらになります。
278
+
279
+ ```ここに言語を入力
280
+
281
+ class Product < ApplicationRecord
282
+
283
+ validates :title,presence: true
284
+
285
+ validates :image,presence: true
286
+
287
+ validates :description,presence: true
288
+
289
+
290
+
291
+ belongs_to :order
292
+
293
+ belongs_to :user
294
+
295
+ has_many:carts
296
+
297
+ has_many :cart_users, through: :carts, source: 'user'
298
+
299
+ mount_uploader :image, ImageUploader
300
+
301
+ end
302
+
303
+ ```

2

必要情報の追加

2019/09/03 11:57

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -144,6 +144,130 @@
144
144
 
145
145
 
146
146
 
147
+ 商品を登録するview(products/new)がこちらになります
148
+
149
+ ```ここに言語を入力
150
+
151
+ <div class="topic-new-wrapper" >
152
+
153
+ <div class="container">
154
+
155
+ <div class="row">
156
+
157
+ <div class="col-md-6 col-md-offset-3">
158
+
159
+ <h1 class="text-center">商品の登録</h1>
160
+
161
+ <%= form_for @product do |f| %>
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+ <div class="form-group">
170
+
171
+ <%= f.label "タイトル" %>
172
+
173
+ <%= f.text_field :title%>
174
+
175
+ </div>
176
+
177
+
178
+
179
+ <div class="form-group">
180
+
181
+ <%= f.label "¥" %>
182
+
183
+ <%= f.number_field :price%>
184
+
185
+ </div>
186
+
187
+
188
+
189
+ <div class="form-group">
190
+
191
+ <%= f.label "画像の登録" %>
192
+
193
+ <%= f.file_field :image%>
194
+
195
+ </div>
196
+
197
+
198
+
199
+ <div class="form-group">
200
+
201
+ <%= f.label "商品の概要" %>
202
+
203
+ <%= f.text_area :description, class: 'form-control' %>
204
+
205
+ </div>
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+ <%= f.submit '投稿', class: 'btn btn-black btn-block' %>
216
+
217
+
218
+
219
+ <% end %>
220
+
221
+ </div>
222
+
223
+ </div>
224
+
225
+ </div>
226
+
227
+ </div>
228
+
229
+ ```
230
+
231
+
232
+
233
+ 商品(product)のマイグレーションファイルがこちらになります。
234
+
235
+ ```ここに言語を入力
236
+
237
+ class CreateProducts < ActiveRecord::Migration[5.2]
238
+
239
+ def change
240
+
241
+ create_table :products do |t|
242
+
243
+ t.string :title
244
+
245
+ t.string :image
246
+
247
+ t.string :description
248
+
249
+ t.integer :price
250
+
251
+ t.integer :user_id
252
+
253
+ t.integer :quanity
254
+
255
+
256
+
257
+ t.timestamps
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ ```
268
+
269
+
270
+
147
271
  投稿に失敗した時のpumaの動きが、こちらになります。
148
272
 
149
273
  ![イメージ説明](e322c320f32a5ae2e783b1ad7d6fe2b6.png)

1

書式を改善しました。よろしくお願いいたします。

2019/09/02 11:35

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -6,20 +6,144 @@
6
6
 
7
7
 
8
8
 
9
- 画面表示はこちらになります。
10
9
 
10
+
11
+ productsコントローラーがこちらになります。
12
+
13
+ ```
14
+
15
+ class ProductsController < ApplicationController
16
+
11
- https://gyazo.com/9fad016bb384acc61f8e5ae7ade48be1
17
+ before_action :find_post, only:[:show,:edit,:update,:destroy]
12
18
 
13
19
 
14
20
 
15
- productsコントローラーが、画像2枚で1つになります。
21
+ def home
16
22
 
17
- https://gyazo.com/641b5bb837c736de16d3a84769dfc643
23
+
18
24
 
25
+ end
26
+
27
+
28
+
29
+ def index
30
+
31
+ @products =Product.all
32
+
33
+ @cart=Cart.new
34
+
35
+ end
36
+
37
+
38
+
39
+ def new
40
+
41
+ @product =Product.new
42
+
43
+ end
44
+
45
+
46
+
47
+ def create
48
+
49
+ @product = current_user.products.new(product_params)
50
+
51
+
52
+
53
+ if @product.save
54
+
55
+ redirect_to products_path,success:"商品登録に成功しました"
56
+
57
+ else
58
+
59
+ flash.now[:danger]="商品登録に失敗しました"
60
+
61
+ render :new
62
+
63
+ end
64
+
65
+ end
66
+
67
+
68
+
69
+ def show
70
+
71
+  # @product = Product.find(params[:id])
72
+
73
+ end
74
+
75
+
76
+
77
+ def edit
78
+
79
+ #@product = Product.find(params[:id])
80
+
81
+
82
+
83
+ end
84
+
85
+
86
+
87
+ def update
88
+
89
+ # @product = Product.find(params[:id])
90
+
91
+ @product.update(product_params)
92
+
93
+ if @product.save
94
+
95
+ redirect_to products_path,success:'編集に成功しました'
96
+
97
+ end
98
+
99
+ end
100
+
101
+
102
+
103
+ def destroy
104
+
105
+ # @product = Product.find(params[:id])
106
+
107
+ if @product.destroy
108
+
109
+ redirect_to products_path,success:'商品を削除しました'
110
+
111
+ end
112
+
113
+ end
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+ private
126
+
127
+ def product_params
128
+
19
- https://gyazo.com/f70d7bb9a5b1ef2b0de3535e21a51638
129
+ params.require(:product).permit(:title,:image,:description,:price)
130
+
131
+ end
132
+
133
+ def find_post
134
+
135
+ @product = Product.find(params[:id])
136
+
137
+ end
138
+
139
+
140
+
141
+ end
142
+
143
+ ```
20
144
 
21
145
 
22
146
 
23
147
  投稿に失敗した時のpumaの動きが、こちらになります。
24
148
 
25
- https://gyazo.com/9a5d1bbf40dc30c832b20d6e66cb91f0
149
+ ![イメージ説明](e322c320f32a5ae2e783b1ad7d6fe2b6.png)