質問編集履歴
4
修正点の編集
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
t.string :cosplay_image_id
|
|
38
38
|
t.text :caption
|
|
39
39
|
t.integer :user_id
|
|
40
|
+
t.integer :favorites_count
|
|
40
41
|
|
|
41
42
|
t.timestamps
|
|
42
43
|
end
|
|
@@ -155,7 +156,7 @@
|
|
|
155
156
|
private
|
|
156
157
|
|
|
157
158
|
def set_variables
|
|
158
|
-
@post_image = PostImage.find(params[:
|
|
159
|
+
@post_image = PostImage.find(params[:id])
|
|
159
160
|
@id_name = "#favorite-link-#{@post_image.id}"
|
|
160
161
|
end
|
|
161
162
|
end
|
|
@@ -185,7 +186,7 @@
|
|
|
185
186
|
private
|
|
186
187
|
|
|
187
188
|
def post_image_params
|
|
188
|
-
params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption)
|
|
189
|
+
params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count)
|
|
189
190
|
end
|
|
190
191
|
end
|
|
191
192
|
```
|
|
@@ -250,16 +251,12 @@
|
|
|
250
251
|
<p><%= post_image.caption %></p>
|
|
251
252
|
<a href="#">23 コメント</a>
|
|
252
253
|
|
|
253
|
-
<%= render partial: 'post_images/post_images',
|
|
254
|
+
<%= render partial: 'post_images/post_images', locals: { post_image: post_image } %>
|
|
254
255
|
|
|
255
256
|
<% end %>
|
|
256
257
|
</div>
|
|
257
258
|
```
|
|
258
259
|
####**部分テンプレート**
|
|
259
|
-
######views/post_images/index.html.erb
|
|
260
|
-
```
|
|
261
|
-
<%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
|
|
262
|
-
```
|
|
263
260
|
######views/post_images/_post_images.html.erb
|
|
264
261
|
```
|
|
265
262
|
<%= render 'favorites/favorite', post_image: post_image %>
|
3
マイグレーションファイルの追加
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
・いいねボタンがたくさん出てしまっている
|
|
13
13
|
|
|
14
14
|
### 該当のソースコード
|
|
15
|
+
####マイグレーションファイル
|
|
15
16
|
######**favoritesテーブル(20200112070415_create_favorites.rb)**
|
|
16
17
|
```
|
|
17
18
|
class CreateFavorites < ActiveRecord::Migration[5.2]
|
|
@@ -25,6 +26,70 @@
|
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
```
|
|
29
|
+
######post_imagesテーブル
|
|
30
|
+
```
|
|
31
|
+
class CreatePostImages < ActiveRecord::Migration[5.2]
|
|
32
|
+
def change
|
|
33
|
+
create_table :post_images do |t|
|
|
34
|
+
t.text :real_image_name
|
|
35
|
+
t.text :cosplay_image_name
|
|
36
|
+
t.string :real_image_id
|
|
37
|
+
t.string :cosplay_image_id
|
|
38
|
+
t.text :caption
|
|
39
|
+
t.integer :user_id
|
|
40
|
+
|
|
41
|
+
t.timestamps
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
######deviseユーザーテーブル
|
|
47
|
+
```
|
|
48
|
+
# frozen_string_literal: true
|
|
49
|
+
|
|
50
|
+
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
|
|
51
|
+
def change
|
|
52
|
+
create_table :users do |t|
|
|
53
|
+
## Database authenticatable
|
|
54
|
+
t.string :email, null: false, default: ""
|
|
55
|
+
t.string :encrypted_password, null: false, default: ""
|
|
56
|
+
|
|
57
|
+
## Recoverable
|
|
58
|
+
t.string :reset_password_token
|
|
59
|
+
t.datetime :reset_password_sent_at
|
|
60
|
+
|
|
61
|
+
## Rememberable
|
|
62
|
+
t.datetime :remember_created_at
|
|
63
|
+
|
|
64
|
+
## Trackable
|
|
65
|
+
# t.integer :sign_in_count, default: 0, null: false
|
|
66
|
+
# t.datetime :current_sign_in_at
|
|
67
|
+
# t.datetime :last_sign_in_at
|
|
68
|
+
# t.string :current_sign_in_ip
|
|
69
|
+
# t.string :last_sign_in_ip
|
|
70
|
+
|
|
71
|
+
## Confirmable
|
|
72
|
+
# t.string :confirmation_token
|
|
73
|
+
# t.datetime :confirmed_at
|
|
74
|
+
# t.datetime :confirmation_sent_at
|
|
75
|
+
# t.string :unconfirmed_email # Only if using reconfirmable
|
|
76
|
+
|
|
77
|
+
## Lockable
|
|
78
|
+
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
|
|
79
|
+
# t.string :unlock_token # Only if unlock strategy is :email or :both
|
|
80
|
+
# t.datetime :locked_at
|
|
81
|
+
|
|
82
|
+
t.string :name
|
|
83
|
+
t.timestamps null: false
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
add_index :users, :email, unique: true
|
|
87
|
+
add_index :users, :reset_password_token, unique: true
|
|
88
|
+
# add_index :users, :confirmation_token, unique: true
|
|
89
|
+
# add_index :users, :unlock_token, unique: true
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
```
|
|
28
93
|
####**モデル**
|
|
29
94
|
######favorite.rb
|
|
30
95
|
```
|
2
ビュー、コントローラーの追加
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -127,14 +127,11 @@
|
|
|
127
127
|
######application_controller.rb
|
|
128
128
|
```
|
|
129
129
|
class ApplicationController < ActionController::Base
|
|
130
|
-
# deviseの機能が使われる場合、その前にconfigure_permitted_parametersが実行される
|
|
131
130
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
|
132
|
-
# ログインユーザーのみ操作を許可する
|
|
133
131
|
before_action :authenticate_user!
|
|
134
132
|
|
|
135
133
|
protected
|
|
136
134
|
|
|
137
|
-
# ユーザー登録の際に、nameの操作が許可される
|
|
138
135
|
def configure_permitted_parameters
|
|
139
136
|
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
|
|
140
137
|
end
|
1
ビュー、コントローラーの追加
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -95,16 +95,114 @@
|
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
```
|
|
98
|
+
######post_images_controller.rb
|
|
99
|
+
```
|
|
100
|
+
class PostImagesController < ApplicationController
|
|
101
|
+
def new
|
|
102
|
+
@post_image = PostImage.new
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def create
|
|
106
|
+
@post_image = PostImage.new(post_image_params)
|
|
107
|
+
@post_image.user_id = current_user.id
|
|
108
|
+
@post_image.save
|
|
109
|
+
redirect_to post_images_path
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def index
|
|
113
|
+
@post_images = PostImage.all
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def show
|
|
117
|
+
@post_image = PostImage.find(params[:id])
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
def post_image_params
|
|
123
|
+
params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
```
|
|
127
|
+
######application_controller.rb
|
|
128
|
+
```
|
|
129
|
+
class ApplicationController < ActionController::Base
|
|
130
|
+
# deviseの機能が使われる場合、その前にconfigure_permitted_parametersが実行される
|
|
131
|
+
before_action :configure_permitted_parameters, if: :devise_controller?
|
|
132
|
+
# ログインユーザーのみ操作を許可する
|
|
133
|
+
before_action :authenticate_user!
|
|
134
|
+
|
|
135
|
+
protected
|
|
136
|
+
|
|
137
|
+
# ユーザー登録の際に、nameの操作が許可される
|
|
138
|
+
def configure_permitted_parameters
|
|
139
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
```
|
|
143
|
+
####ビュー
|
|
144
|
+
######postimages/index.html.erb
|
|
145
|
+
```
|
|
146
|
+
<div class="header">
|
|
147
|
+
<nav class="navigation">
|
|
148
|
+
<img src="/assets/logo.png">
|
|
149
|
+
<ul>
|
|
150
|
+
<li>
|
|
151
|
+
<%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
|
|
152
|
+
</li>
|
|
153
|
+
|
|
154
|
+
<li>
|
|
155
|
+
<%= link_to '投稿する', new_post_image_path %>
|
|
156
|
+
</li>
|
|
157
|
+
|
|
158
|
+
<li>
|
|
159
|
+
<%= link_to 'マイページ', '#' %>
|
|
160
|
+
</li>
|
|
161
|
+
</ul>
|
|
162
|
+
</nav>
|
|
163
|
+
</div>
|
|
164
|
+
<div class="post_images_index_wrapper">
|
|
165
|
+
<% @post_images.each do |post_image| %>
|
|
166
|
+
<div class="post_images_index_user">
|
|
167
|
+
<ul>
|
|
168
|
+
<li><%= image_tag('no_image.jpg') %></li>
|
|
169
|
+
<li><p><%= post_image.user.name %></p></li>
|
|
170
|
+
</ul>
|
|
171
|
+
</div>
|
|
172
|
+
<div class="post_images_box">
|
|
173
|
+
<div class="post_image">
|
|
174
|
+
<%= attachment_image_tag post_image, :real_image %>
|
|
175
|
+
</div>
|
|
176
|
+
<div class="post_image">
|
|
177
|
+
<%= attachment_image_tag post_image, :cosplay_image %>
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
<h3 class="block-title">
|
|
181
|
+
<%= link_to post_image_path(post_image.id) do %>
|
|
182
|
+
<%= post_image.real_image_name %>
|
|
183
|
+
<% end %>
|
|
184
|
+
<%= link_to post_image_path(post_image.id) do %>
|
|
185
|
+
<%= post_image.cosplay_image_name %>
|
|
186
|
+
<% end %>
|
|
187
|
+
</h3>
|
|
188
|
+
<p><%= post_image.caption %></p>
|
|
189
|
+
<a href="#">23 コメント</a>
|
|
190
|
+
|
|
191
|
+
<%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
|
|
192
|
+
|
|
193
|
+
<% end %>
|
|
194
|
+
</div>
|
|
195
|
+
```
|
|
98
196
|
####**部分テンプレート**
|
|
99
197
|
######views/post_images/index.html.erb
|
|
100
198
|
```
|
|
101
199
|
<%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
|
|
102
200
|
```
|
|
103
|
-
views/post_images/_post_images.html.erb
|
|
201
|
+
######views/post_images/_post_images.html.erb
|
|
104
202
|
```
|
|
105
203
|
<%= render 'favorites/favorite', post_image: post_image %>
|
|
106
204
|
```
|
|
107
|
-
views/favorites/_favorite.html.erb
|
|
205
|
+
######views/favorites/_favorite.html.erb
|
|
108
206
|
```
|
|
109
207
|
<div class="favorite-link" id="favorite-link-<%= post_image.id %>">
|
|
110
208
|
<% if current_user.favorites.find_by(post_image_id: post_image.id) %>
|