質問編集履歴

1

完了

2020/05/15 00:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,597 +1,3 @@
1
- image_tagプロフィール画像を表示させようと試みていますが、以下のようなエラーが表示されます
1
+ 修正きたため完了
2
2
 
3
- ![イメージ説明](ee58640ce2492a618f6147087b2a6563.png)
4
-
5
- データベースでの保存形式も以下のようになってしまいます。
6
-
7
- usersテーブル
8
-
9
- ![イメージ説明](e6d4c57a30c39024be147f65857367b3.png)
10
-
11
- tweetsテーブルは正常に保存もできていますし、image_tagで表示もできています。
12
-
13
- ![イメージ説明](27d225a7a34dbeff47d17bd14e799709.png)
14
-
15
-
16
-
17
- ```ここに言語を入力
18
-
19
- #usersテーブル(devise)のDB
20
-
21
-
22
-
23
- class DeviseCreateUsers < ActiveRecord::Migration[5.2]
24
-
25
- def change
26
-
27
- create_table :users do |t|
28
-
29
- ## Database authenticatable
30
-
31
- t.string :name, null:false
32
-
33
- t.string :usernoid, null:false
34
-
35
- t.text :profile
36
-
37
- t.text :image
38
-
39
- t.string :sex
40
-
41
- t.integer :age
42
-
43
- t.integer :tall
44
-
45
- t.string :email, null: false, default: ""
46
-
47
- t.string :encrypted_password, null: false, default: ""
48
-
49
-
50
-
51
- ## Recoverable
52
-
53
- t.string :reset_password_token
54
-
55
- t.datetime :reset_password_sent_at
56
-
57
-
58
-
59
- ## Rememberable
60
-
61
- t.datetime :remember_created_at
62
-
63
-
64
-
65
- ## Trackable
66
-
67
- # t.integer :sign_in_count, default: 0, null: false
68
-
69
- # t.datetime :current_sign_in_at
70
-
71
- # t.datetime :last_sign_in_at
72
-
73
- # t.string :current_sign_in_ip
74
-
75
- # t.string :last_sign_in_ip
76
-
77
-
78
-
79
- ## Confirmable
80
-
81
- # t.string :confirmation_token
82
-
83
- # t.datetime :confirmed_at
84
-
85
- # t.datetime :confirmation_sent_at
86
-
87
- # t.string :unconfirmed_email # Only if using reconfirmable
88
-
89
-
90
-
91
- ## Lockable
92
-
93
- # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
94
-
95
- # t.string :unlock_token # Only if unlock strategy is :email or :both
96
-
97
- # t.datetime :locked_at
98
-
99
-
100
-
101
-
102
-
103
- t.timestamps null: false
104
-
105
-
106
-
107
- end
108
-
109
- # add_index :users, :name
110
-
111
- # add_index :users, :useruserid, unique: true
112
-
113
- # add_index :users, :email, unique: true
114
-
115
- # add_index :users, :reset_password_token, unique: true
116
-
117
- # add_index :users, :confirmation_token, unique: true
118
-
119
- # add_index :users, :unlock_token, unique: true
120
-
121
- end
122
-
123
- end
124
-
125
-
126
-
127
-
128
-
129
- #tweetテーブルのDB
130
-
131
-
132
-
133
- class CreateTweets < ActiveRecord::Migration[5.2]
134
-
135
- def change
136
-
137
- create_table :tweets do |t|
138
-
139
- t.text :text,null: false
140
-
141
- t.string :title,null: false
142
-
143
- t.string :sex,null:false
144
-
145
- t.integer :tall,null: false
146
-
147
- t.string :season,null:false
148
-
149
- t.text :image,null:false
150
-
151
- t.integer :temp,null:false
152
-
153
- t.string :typestyle,null:false
154
-
155
- t.integer :user_id
156
-
157
- t.timestamps
158
-
159
- end
160
-
161
- end
162
-
163
- end
164
-
165
-
166
-
167
- #user.rb
168
-
169
-
170
-
171
- class User < ApplicationRecord
172
-
173
- # Include default devise modules. Others available are:
174
-
175
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
176
-
177
- devise :database_authenticatable, :registerable,
178
-
179
- :recoverable, :rememberable, :validatable
180
-
181
-
182
-
183
- validates :usernoid, presence: true, uniqueness: true
184
-
185
- has_many :tweets
186
-
187
- has_many :likes, dependent: :destroy
188
-
189
- has_many :liked_tweets, through: :likes, source: :tweet
190
-
191
- has_many :comments
192
-
193
-
194
-
195
-
196
-
197
- def already_liked?(tweet)
198
-
199
- self.likes.exists?(tweet_id: tweet.id)
200
-
201
- end
202
-
203
-
204
-
205
- def self.search(search)
206
-
207
- if search
208
-
209
- User.where('name LIKE(?) or sex LIKE(?) ', "%#{search}%","%#{search}%")
210
-
211
- else
212
-
213
- User.all
214
-
215
- end
216
-
217
- end
218
-
219
-
220
-
221
-
222
-
223
- def update_without_current_password(params, *options)
224
-
225
- params.delete(:current_password)
226
-
227
-
228
-
229
- if params[:password].blank? && params[:password_confirmation].blank?
230
-
231
- params.delete(:password)
232
-
233
- params.delete(:password_confirmation)
234
-
235
- end
236
-
237
-
238
-
239
- result = update_attributes(params, *options)
240
-
241
- clean_up_passwords
242
-
243
- result
244
-
245
- end
246
-
247
- end
248
-
249
-
250
-
251
- #tweet.rb
252
-
253
-
254
-
255
- class Tweet < ApplicationRecord
256
-
257
- # belongs_to :user
258
-
259
- # has_many :tweets
260
-
261
- validates :image,:text,:sex,:tall,:title,:temp, presence: true
262
-
263
- mount_uploader :image, ImageUploader
3
+ 原因はuser.rbにmount_uploader :image, ImageUploaderの記載がなかったため
264
-
265
- belongs_to :user
266
-
267
- has_many :comments
268
-
269
- has_many :likes
270
-
271
- has_many :liked_users, through: :likes, source: :user
272
-
273
- # has_many :temps
274
-
275
-
276
-
277
-
278
-
279
- def self.search(search)
280
-
281
- if search
282
-
283
- Tweet.where('text LIKE(?) or title LIKE(?) or sex LIKE(?) or typestyle LIKE(?)',"%#{search}%", "%#{search}%", "%#{search}%","%#{search}%")
284
-
285
- else
286
-
287
- Tweet.all
288
-
289
- end
290
-
291
- end
292
-
293
-
294
-
295
- end
296
-
297
-
298
-
299
-
300
-
301
- #users.controller.rb
302
-
303
-
304
-
305
- class UsersController < ApplicationController
306
-
307
- def index
308
-
309
- @users=User.all.order(created_at: :desc).page(params[:page]).per(20)
310
-
311
- end
312
-
313
- def new
314
-
315
- @user = User.new
316
-
317
- end
318
-
319
- def created
320
-
321
- end
322
-
323
- def show
324
-
325
- @user = User.find(params[:id])
326
-
327
- # @tweets=@user.tweets
328
-
329
- end
330
-
331
-
332
-
333
-
334
-
335
- def search
336
-
337
- @user = User.search(params[:keyword]).order(created_at: :desc).page(params[:page]).per(20)
338
-
339
- end
340
-
341
-
342
-
343
-
344
-
345
- end
346
-
347
-
348
-
349
-
350
-
351
- #tweets.controller.rb
352
-
353
-
354
-
355
- class TweetsController < ApplicationController
356
-
357
-
358
-
359
- def index
360
-
361
- @tweets = Tweet.all.order(created_at: :desc).page(params[:page]).per(20)
362
-
363
- @tweet=Tweet.new
364
-
365
- tweet_like_count = Tweet.joins(:likes).group(:tweet_id).count
366
-
367
- tweet_liked_ids = Hash[tweet_like_count.sort_by{ |_, v| -v }].keys
368
-
369
- @tweet_ranking= Tweet.where(id: tweet_liked_ids).limit(5)
370
-
371
- end
372
-
373
-
374
-
375
- def new
376
-
377
- @tweet = Tweet.new
378
-
379
- end
380
-
381
- def create
382
-
383
- @tweet = Tweet.create(tweet_params)
384
-
385
- end
386
-
387
- def destroy
388
-
389
- tweet = Tweet.find(params[:id])
390
-
391
- tweet.destroy
392
-
393
- end
394
-
395
- def edit
396
-
397
- @tweet = Tweet.find(params[:id])
398
-
399
- end
400
-
401
- def update
402
-
403
- tweet = Tweet.find(params[:id])
404
-
405
- tweet.update(tweet_params)
406
-
407
- end
408
-
409
-
410
-
411
- def show
412
-
413
- @tweet = Tweet.find(params[:id])
414
-
415
- @like = Like.new
416
-
417
- @comment = Comment.new
418
-
419
- @comments = @tweet.comments.includes(:user)
420
-
421
- end
422
-
423
-
424
-
425
- def search
426
-
427
- @tweets = Tweet.search(params[:keyword]).order(created_at: :desc).page(params[:page]).per(20)
428
-
429
- @tweet=Tweet.new
430
-
431
- tweet_like_count = Tweet.joins(:likes).group(:tweet_id).count
432
-
433
- tweet_liked_ids = Hash[tweet_like_count.sort_by{ |_, v| -v }].keys
434
-
435
- @tweet_ranking= Tweet.where(id: tweet_liked_ids).limit(5)
436
-
437
- end
438
-
439
-
440
-
441
-
442
-
443
- private
444
-
445
- def tweet_params
446
-
447
- params.require(:tweet).permit(:name,:image,:title,:season,:text,:sex,:tall,:temp,:typestyle).merge(user_id: current_user.id)
448
-
449
- end
450
-
451
-
452
-
453
-
454
-
455
- end
456
-
457
-
458
-
459
- #users.show.html
460
-
461
-
462
-
463
- .useriine
464
-
465
- .useriine-lines
466
-
467
- .useriine-lines-prof
468
-
469
- .useriine-lines-prof-sub
470
-
471
- .useriine-lines-prof-sub-image
472
-
473
- %p.useriine-lines-prof-sub-image-users
474
-
475
- = image_tag(@user.image_url)
476
-
477
- .useriine-lines-prof-sub-profile
478
-
479
- - if current_user == @user
480
-
481
- %p.useriine-lines-prof-sub-profile-name
482
-
483
- = link_to edit_user_registration_path do
484
-
485
- プロフィール編集
486
-
487
-
488
-
489
- #tweets.index.html.haml
490
-
491
-
492
-
493
- .user_rank
494
-
495
- - @tweet_ranking.length >= 0
496
-
497
- - @tweet_ranking.each.with_index(1) do |tweet, i|
498
-
499
- .user_rank_ranks
500
-
501
-
502
-
503
- = i
504
-
505
-
506
-
507
- .user_rank_prof
508
-
509
- = link_to tweet.title, tweet_path(tweet.id)
510
-
511
- .user_rank_show
512
-
513
- .user_rank_show_image
514
-
515
- %p.user_rank_show_image_names
516
-
517
- =image_tag(tweet.image_url)
518
-
519
- .user_rank_show_name
520
-
521
- %a{:href => "/users/#{tweet.user.id}"}= tweet.user.name
522
-
523
-
524
-
525
- #routes.rb
526
-
527
-
528
-
529
- Rails.application.routes.draw do
530
-
531
- devise_for :users, :controllers => {
532
-
533
- registrations: 'users/registrations',
534
-
535
- sessions: 'users/sessions'
536
-
537
- }
538
-
539
- get 'tweets/index'
540
-
541
- root "tweets#index"
542
-
543
- resources :tweets do
544
-
545
- resources :likes, only: [:create, :destroy]
546
-
547
- resources :comments, only: :create
548
-
549
- collection do
550
-
551
- get 'search'
552
-
553
- end
554
-
555
- collection do
556
-
557
- get 'about'
558
-
559
- end
560
-
561
- collection do
562
-
563
- get 'kiyaku'
564
-
565
- end
566
-
567
- collection do
568
-
569
- get 'privacy'
570
-
571
- end
572
-
573
- end
574
-
575
- resources :users do
576
-
577
- collection do
578
-
579
- get 'search'
580
-
581
- end
582
-
583
- member do
584
-
585
- get :profile
586
-
587
- end
588
-
589
- end
590
-
591
- end
592
-
593
-
594
-
595
- ```
596
-
597
- 調べても表示できない、DBへの保存形式がこのようになるのがわかりませんでしたので質問させていただきました。