質問編集履歴
1
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,40 +10,178 @@
|
|
10
10
|
|
11
11
|
rails初心者で、わかりやすく教えて頂けたら幸いです。よろしくお願いします。
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
[エラー内容](https://gyazo.com/8744816f4433203faa4fcfe6bbc17434)
|
16
|
-
|
17
|
-
message.rb
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
```
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
|
22
|
-
|
17
|
+
shop.rb
|
18
|
+
|
19
|
+
```
|
20
|
+
|
23
|
-
class
|
21
|
+
class Shop < ApplicationRecord
|
22
|
+
|
24
|
-
|
23
|
+
geocoded_by :address
|
24
|
+
|
25
|
-
|
25
|
+
after_validation :geocode, if: :address_changed?
|
26
|
+
|
27
|
+
has_many :messages
|
26
28
|
|
27
29
|
belongs_to :user, optional: true
|
28
30
|
|
31
|
+
geocoded_by :address
|
32
|
+
|
33
|
+
after_validation :geocode
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
user.rb
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
class User < ApplicationRecord
|
48
|
+
|
49
|
+
# Include default devise modules. Others available are:
|
50
|
+
|
51
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
52
|
+
|
53
|
+
devise :database_authenticatable, :registerable,
|
54
|
+
|
55
|
+
:recoverable, :rememberable, :validatable
|
56
|
+
|
57
|
+
has_many :messages
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
messages.controller
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
class MessagesController < ApplicationController
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
before_action :move_to_index, except: :index
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
def index
|
80
|
+
|
81
|
+
@messages = Message.order("created_at DESC").page(params[:page]).per(6)
|
82
|
+
|
83
|
+
|
84
|
+
|
29
|
-
|
85
|
+
respond_to do |format|
|
86
|
+
|
87
|
+
format.html
|
88
|
+
|
89
|
+
format.js
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def new
|
98
|
+
|
99
|
+
@shop = Shop.find(params[:id])
|
100
|
+
|
101
|
+
@message = @shop.messages.build
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def create
|
108
|
+
|
109
|
+
@messages = Message.new(messages_params)
|
110
|
+
|
111
|
+
@messages.save
|
112
|
+
|
113
|
+
end
|
30
114
|
|
31
115
|
|
32
116
|
|
117
|
+
|
118
|
+
|
119
|
+
def show
|
120
|
+
|
33
|
-
ac
|
121
|
+
@nickname = current_user.nickname
|
34
|
-
|
122
|
+
|
35
|
-
|
123
|
+
@messages = Message.find(params[:id])
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
124
|
+
|
40
|
-
|
41
|
-
likes.find_by(user_id: user_id)
|
42
|
-
|
43
|
-
end
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def destroy
|
132
|
+
|
133
|
+
message = messages.find(params[:shop_id])
|
134
|
+
|
135
|
+
message.destroy if message.user_id == current_user.id
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def edit
|
142
|
+
|
143
|
+
@messages = Message.find(params[:id])
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def update
|
150
|
+
|
151
|
+
message = Message.find(params[:id])
|
152
|
+
|
153
|
+
if message.user_id == current_user.id
|
154
|
+
|
155
|
+
message.update(messages_params)
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
private
|
44
164
|
|
45
165
|
|
46
166
|
|
167
|
+
def messages_params
|
168
|
+
|
169
|
+
params.require(:message).permit(:text, :image, shop_attributes:[:name, :address, :latitude, :longitude, :message_id ])
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
def move_to_index
|
178
|
+
|
179
|
+
redirect_to action: :index unless user_signed_in?
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
47
185
|
end
|
48
186
|
|
49
187
|
|
@@ -52,340 +190,18 @@
|
|
52
190
|
|
53
191
|
|
54
192
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
after_validation :geocode
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
```
|
78
|
-
|
79
|
-
user.rb
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
```
|
84
|
-
|
85
|
-
class User < ApplicationRecord
|
86
|
-
|
87
|
-
# Include default devise modules. Others available are:
|
88
|
-
|
89
|
-
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
90
|
-
|
91
|
-
devise :database_authenticatable, :registerable,
|
92
|
-
|
93
|
-
:recoverable, :rememberable, :validatable
|
94
|
-
|
95
|
-
has_many :messages
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
```
|
102
|
-
|
103
|
-
messages.controller
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
```
|
108
|
-
|
109
|
-
class MessagesController < ApplicationController
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
before_action :move_to_index, except: :index
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def index
|
118
|
-
|
119
|
-
@messages = Message.order("created_at DESC").page(params[:page]).per(6)
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
respond_to do |format|
|
124
|
-
|
125
|
-
format.html
|
126
|
-
|
127
|
-
format.js
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
def new
|
136
|
-
|
137
|
-
@shop = Shop.find(params[:id])
|
138
|
-
|
139
|
-
@message = @shop.messages.build
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
def create
|
146
|
-
|
147
|
-
@messages = Message.new(messages_params)
|
148
|
-
|
149
|
-
@messages.save
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
def show
|
158
|
-
|
159
|
-
@nickname = current_user.nickname
|
160
|
-
|
161
|
-
@messages = Message.find(params[:id])
|
162
|
-
|
163
|
-
end
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
def destroy
|
170
|
-
|
171
|
-
message = messages.find(params[:shop_id])
|
172
|
-
|
173
|
-
message.destroy if message.user_id == current_user.id
|
174
|
-
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
def edit
|
180
|
-
|
181
|
-
@messages = Message.find(params[:id])
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
def update
|
188
|
-
|
189
|
-
message = Message.find(params[:id])
|
190
|
-
|
191
|
-
if message.user_id == current_user.id
|
192
|
-
|
193
|
-
message.update(messages_params)
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
private
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
def messages_params
|
206
|
-
|
207
|
-
params.require(:message).permit(:text, :image, shop_attributes:[:name, :address, :latitude, :longitude, :message_id ])
|
208
|
-
|
209
|
-
end
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
def move_to_index
|
216
|
-
|
217
|
-
redirect_to action: :index unless user_signed_in?
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
end
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
```
|
228
|
-
|
229
|
-
new.html.erb
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
```
|
234
|
-
|
235
|
-
<div class="contents" >
|
236
|
-
|
237
|
-
<h3>新しい投稿</h3>
|
238
|
-
|
239
|
-
<%= form_with(model: @messages, local: true) do |form| %>
|
240
|
-
|
241
|
-
<div class="form">
|
242
|
-
|
243
|
-
<div class="form-group">
|
244
|
-
|
245
|
-
<div id="img_field" onClick="$('#file').click()" >
|
246
|
-
|
247
|
-
<% if @messages.image.url.present? %>
|
248
|
-
|
249
|
-
<%= image_tag(@messages.image.url) %>
|
250
|
-
|
251
|
-
<% else %>
|
252
|
-
|
253
|
-
<i class="fas fa-camera"></i></i>
|
254
|
-
|
255
|
-
<% end %>
|
256
|
-
|
257
|
-
</div>
|
258
|
-
|
259
|
-
<%= form.file_field :image, style: "display:none;", id: "file"%>
|
260
|
-
|
261
|
-
</div>
|
262
|
-
|
263
|
-
<div class="form-group">
|
264
|
-
|
265
|
-
<div class="cp_iptxt">
|
266
|
-
|
267
|
-
<label class="ef">
|
268
|
-
|
269
|
-
<%= form.text_area :text, cols: "10", rows: "10", placeholder: "コメント" %>
|
270
|
-
|
271
|
-
</label>
|
272
|
-
|
273
|
-
</div>
|
274
|
-
|
275
|
-
<%= fields_for :shop do |i| %>
|
276
|
-
|
277
|
-
<div class="cp_iptxt">
|
278
|
-
|
279
|
-
<label class="ef">
|
280
|
-
|
281
|
-
<%= i.text_area :name, class: "ef", placeholder: "サロン名" %>
|
282
|
-
|
283
|
-
</label>
|
284
|
-
|
285
|
-
</div>
|
286
|
-
|
287
|
-
<div class="cp_iptxt">
|
288
|
-
|
289
|
-
<label class="ef">
|
290
|
-
|
291
|
-
<%= i.text_area :address, placeholder: "サロンの住所" %>
|
292
|
-
|
293
|
-
</label>
|
294
|
-
|
295
|
-
</div>
|
296
|
-
|
297
|
-
</div>
|
298
|
-
|
299
|
-
<% end %>
|
300
|
-
|
301
|
-
</div>
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
<%= form.submit "S E N D", class:"button"%>
|
306
|
-
|
307
|
-
<% end %>
|
308
|
-
|
309
|
-
</div>
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
```
|
316
|
-
|
317
|
-
shop.マグレーション
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
```
|
322
|
-
|
323
|
-
class CreateShops < ActiveRecord::Migration[5.2]
|
324
|
-
|
325
|
-
def change
|
326
|
-
|
327
|
-
create_table :shops do |t|
|
328
|
-
|
329
|
-
t.string "name"
|
330
|
-
|
331
|
-
t.string "prefecture"
|
332
|
-
|
333
|
-
t.string "address_city"
|
334
|
-
|
335
|
-
t.string "address_street"
|
336
|
-
|
337
|
-
t.string "address"
|
338
|
-
|
339
|
-
t.float "latitude"
|
340
|
-
|
341
|
-
t.float "longitude"
|
342
|
-
|
343
|
-
t.integer :user_id
|
344
|
-
|
345
|
-
t.references :message, foreign_key: true
|
346
|
-
|
347
|
-
t.timestamps
|
348
|
-
|
349
|
-
end
|
350
|
-
|
351
|
-
end
|
352
|
-
|
353
|
-
end
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
```
|
360
|
-
|
361
|
-
message.マイグレーション
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
```
|
366
|
-
|
367
|
-
class CreateMessages < ActiveRecord::Migration[5.2]
|
368
|
-
|
369
|
-
def change
|
370
|
-
|
371
|
-
create_table :messages do |t|
|
372
|
-
|
373
|
-
t.string :name
|
374
|
-
|
375
|
-
t.text :text
|
376
|
-
|
377
|
-
t.string :image
|
378
|
-
|
379
|
-
t.timestamps null: true
|
380
|
-
|
381
|
-
end
|
382
|
-
|
383
|
-
end
|
384
|
-
|
385
|
-
end
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
```
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```
|