質問編集履歴

3

ソースコードを追加しました。

2020/01/22 15:16

投稿

auto8726
auto8726

スコア4

test CHANGED
File without changes
test CHANGED
@@ -17,42 +17,6 @@
17
17
  >Posts/index
18
18
 
19
19
  ```Ruby on Rails
20
-
21
- <div id="tinderslide">
22
-
23
- <ul>
24
-
25
- <% @users.each do |user| %>
26
-
27
- <li data-user_id="<%= user.id %>">
28
-
29
- <div class="userName"><%= user.name %></div>
30
-
31
- <%= image_tag user.img_name.url(:thumb) %>
32
-
33
- <div class="like"></div>
34
-
35
- <div class="dislike"></div>
36
-
37
- </li>
38
-
39
- <% end %>
40
-
41
- </ul>
42
-
43
- <div class="noUser">近くにお相手がいません。</div>
44
-
45
- </div>
46
-
47
- <div class="actions" id="actionBtnArea">
48
-
49
- <a href="#" class="dislike"><i class="fas fa-times fa-2x"></i></a>
50
-
51
- <a href="#" class="like"><i class="fas fa-heart fa-2x"></i></a>
52
-
53
- </div>
54
-
55
- </div>
56
20
 
57
21
  <script>
58
22
 

2

ソースコードを追加しました。

2020/01/22 15:16

投稿

auto8726
auto8726

スコア4

test CHANGED
File without changes
test CHANGED
@@ -14,202 +14,190 @@
14
14
 
15
15
 
16
16
 
17
- >Posts.controller.rb
17
+ >Posts/index
18
-
19
-
20
18
 
21
19
  ```Ruby on Rails
22
20
 
23
- class PhotosController < ApplicationController
24
-
25
- before_action :set_current_user_photo, only: [:edit, :update, :destroy]
26
-
27
-
28
-
29
- # GET /photos
30
-
31
- # GET /photos.json
32
-
33
- def index
34
-
35
- @photos = Photo.where.not(id: current_user.id).limit(5)
36
-
37
- end
38
-
39
-
40
-
41
- # GET /photos/1
42
-
43
- # GET /photos/1.json
44
-
45
- def show
46
-
47
- @photo = Photo.find(params[:id])
48
-
49
- end
50
-
51
-
52
-
53
- # GET /photos/new
54
-
55
- def new
56
-
57
- @photo = current_user.photos.build
58
-
59
- end
60
-
61
-
62
-
63
- # GET /photos/1/edit
64
-
65
- def edit
66
-
67
- end
68
-
69
-
70
-
71
- # POST /photos
72
-
73
- # POST /photos.json
74
-
75
- def create
76
-
77
- @photo = Photo.new(photo_params)
78
-
79
-
80
-
81
- respond_to do |format|
82
-
83
- if @photo.save
84
-
85
- format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
86
-
87
- format.json { render :show, status: :created, location: @photo }
88
-
89
- else
90
-
91
- format.html { render :new }
92
-
93
- format.json { render json: @photo.errors, status: :unprocessable_entity }
94
-
95
- end
21
+ <div id="tinderslide">
22
+
23
+ <ul>
24
+
25
+ <% @users.each do |user| %>
26
+
27
+ <li data-user_id="<%= user.id %>">
28
+
29
+ <div class="userName"><%= user.name %></div>
30
+
31
+ <%= image_tag user.img_name.url(:thumb) %>
32
+
33
+ <div class="like"></div>
34
+
35
+ <div class="dislike"></div>
36
+
37
+ </li>
38
+
39
+ <% end %>
40
+
41
+ </ul>
42
+
43
+ <div class="noUser">近くにお相手がいません。</div>
44
+
45
+ </div>
46
+
47
+ <div class="actions" id="actionBtnArea">
48
+
49
+ <a href="#" class="dislike"><i class="fas fa-times fa-2x"></i></a>
50
+
51
+ <a href="#" class="like"><i class="fas fa-heart fa-2x"></i></a>
52
+
53
+ </div>
54
+
55
+ </div>
56
+
57
+ <script>
58
+
59
+ // スワイプするユーザー数の定数を定義
60
+
61
+ var usersNum = <%= @users.length %>
62
+
63
+ var currentUserIndex = 0;
64
+
65
+ var postReaction = function (user_id, reaction) {
66
+
67
+ $.ajaxSetup({
68
+
69
+ headers: {
70
+
71
+ 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
72
+
73
+ }
74
+
75
+ });
76
+
77
+ $.ajax({
78
+
79
+ type: "POST",
80
+
81
+ url: "<%= reactions_path %>",
82
+
83
+ data: {
84
+
85
+ user_id: user_id,
86
+
87
+ reaction: reaction,
88
+
89
+ },
90
+
91
+ success: function(j_data){
92
+
93
+ console.log("success")
94
+
95
+ }
96
+
97
+ });
98
+
99
+ }
100
+
101
+ $("#tinderslide").jTinder({
102
+
103
+ onDislike: function (item) {
104
+
105
+ currentUserIndex++;
106
+
107
+ checkUserNum();
108
+
109
+ var user_id = item[0].dataset.user_id
110
+
111
+ postReaction(user_id, 'dislike')
112
+
113
+ },
114
+
115
+ onLike: function (item) {
116
+
117
+ currentUserIndex++;
118
+
119
+ checkUserNum();
120
+
121
+ var user_id = item[0].dataset.user_id
122
+
123
+ postReaction(user_id, 'like')
124
+
125
+ },
126
+
127
+ animationRevertSpeed: 200,
128
+
129
+ animationSpeed: 400,
130
+
131
+ threshold: 1,
132
+
133
+ likeSelector: '.like',
134
+
135
+ dislikeSelector: '.dislike'
136
+
137
+ });
138
+
139
+ $('.actions .like, .actions .dislike').click(function(e){
140
+
141
+ e.preventDefault();
142
+
143
+ $("#tinderslide").jTinder($(this).attr('class'));
144
+
145
+ });
146
+
147
+
148
+
149
+ function checkUserNum() {
150
+
151
+ // スワイプするユーザー数とスワイプした回数が同じになればaddClassする
152
+
153
+ if (currentUserIndex === usersNum){
154
+
155
+ $(".noUser").addClass("is-active");
156
+
157
+ $("#actionBtnArea").addClass("is-none")
158
+
159
+ return;
160
+
161
+ }
162
+
163
+ }
164
+
165
+ </script>
166
+
167
+ ```
168
+
169
+
170
+
171
+ >photos.controller.erb
172
+
173
+ ```Ruby on Rails
174
+
175
+ class UsersController < ApplicationController
176
+
177
+ # indexアクションを追加する
178
+
179
+ def index
180
+
181
+ @users = User.where.not(id: current_user.id)
96
182
 
97
183
  end
98
184
 
99
- end
185
+
100
-
101
-
102
-
103
- # PATCH/PUT /photos/1
186
+
104
-
105
- # PATCH/PUT /photos/1.json
106
-
107
- def update
187
+ def show
108
-
188
+
109
- respond_to do |format|
189
+ # @userというインスタンス変数を用意する
110
-
190
+
111
- if @photo.update(photo_params)
191
+ @user = User.find_by(id: params[:id])
112
-
113
- format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
114
-
115
- format.json { render :show, status: :ok, location: @photo }
116
-
117
- else
118
-
119
- format.html { render :edit }
120
-
121
- format.json { render json: @photo.errors, status: :unprocessable_entity }
122
-
123
- end
124
192
 
125
193
  end
126
194
 
127
- end
128
-
129
-
130
-
131
- # DELETE /photos/1
132
-
133
- # DELETE /photos/1.json
134
-
135
- def destroy
136
-
137
- @photo.destroy
138
-
139
- respond_to do |format|
140
-
141
- format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
142
-
143
- format.json { head :no_content }
144
-
145
- end
146
-
147
- end
148
-
149
-
150
-
151
- private
152
-
153
- # Use callbacks to share common setup or constraints between actions.
154
-
155
- def set_current_user_photo
156
-
157
- @photo = current_user.photos.find(params[:id])
158
-
159
- end
160
-
161
-
162
-
163
- # Never trust parameters from the scary internet, only allow the white list through.
164
-
165
- def photo_params
166
-
167
- params.require(:photo).permit(:image, :caption, :user_id)
168
-
169
- end
170
-
171
195
  end
172
196
 
173
197
  ```
174
198
 
175
199
 
176
200
 
177
- >Photos/index.html.erb
178
-
179
-
180
-
181
- ```Ruby on Rails
182
-
183
- <div id="tinderslide">
184
-
185
- <ul>
186
-
187
- <% @photos.each do |photo| %>
188
-
189
- <li>
190
-
191
- <div class="photoCaption"><%= photo.caption %></div>
192
-
193
- <%= image_tag(photo.image_url) %>
194
-
195
- <div class="like"></div>
196
-
197
- <div class="dislike"></div>
198
-
199
- </li>
200
-
201
- <% end %>
202
-
203
- </ul>
204
-
205
- <div class="noUser">~~</div>
206
-
207
- </div>
208
-
209
- ```
210
-
211
-
212
-
213
201
  ### 試したこと
214
202
 
215
203
 

1

コードの一部追加を多行いました

2020/01/19 14:27

投稿

auto8726
auto8726

スコア4

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,202 @@
14
14
 
15
15
 
16
16
 
17
+ >Posts.controller.rb
18
+
19
+
20
+
21
+ ```Ruby on Rails
22
+
23
+ class PhotosController < ApplicationController
24
+
25
+ before_action :set_current_user_photo, only: [:edit, :update, :destroy]
26
+
27
+
28
+
29
+ # GET /photos
30
+
31
+ # GET /photos.json
32
+
33
+ def index
34
+
35
+ @photos = Photo.where.not(id: current_user.id).limit(5)
36
+
37
+ end
38
+
39
+
40
+
41
+ # GET /photos/1
42
+
43
+ # GET /photos/1.json
44
+
45
+ def show
46
+
47
+ @photo = Photo.find(params[:id])
48
+
49
+ end
50
+
51
+
52
+
53
+ # GET /photos/new
54
+
55
+ def new
56
+
57
+ @photo = current_user.photos.build
58
+
59
+ end
60
+
61
+
62
+
63
+ # GET /photos/1/edit
64
+
65
+ def edit
66
+
67
+ end
68
+
69
+
70
+
71
+ # POST /photos
72
+
73
+ # POST /photos.json
74
+
75
+ def create
76
+
77
+ @photo = Photo.new(photo_params)
78
+
79
+
80
+
81
+ respond_to do |format|
82
+
83
+ if @photo.save
84
+
85
+ format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
86
+
87
+ format.json { render :show, status: :created, location: @photo }
88
+
89
+ else
90
+
91
+ format.html { render :new }
92
+
93
+ format.json { render json: @photo.errors, status: :unprocessable_entity }
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+
102
+
103
+ # PATCH/PUT /photos/1
104
+
105
+ # PATCH/PUT /photos/1.json
106
+
107
+ def update
108
+
109
+ respond_to do |format|
110
+
111
+ if @photo.update(photo_params)
112
+
113
+ format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
114
+
115
+ format.json { render :show, status: :ok, location: @photo }
116
+
117
+ else
118
+
119
+ format.html { render :edit }
120
+
121
+ format.json { render json: @photo.errors, status: :unprocessable_entity }
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+
130
+
131
+ # DELETE /photos/1
132
+
133
+ # DELETE /photos/1.json
134
+
135
+ def destroy
136
+
137
+ @photo.destroy
138
+
139
+ respond_to do |format|
140
+
141
+ format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
142
+
143
+ format.json { head :no_content }
144
+
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+ private
152
+
153
+ # Use callbacks to share common setup or constraints between actions.
154
+
155
+ def set_current_user_photo
156
+
157
+ @photo = current_user.photos.find(params[:id])
158
+
159
+ end
160
+
161
+
162
+
163
+ # Never trust parameters from the scary internet, only allow the white list through.
164
+
165
+ def photo_params
166
+
167
+ params.require(:photo).permit(:image, :caption, :user_id)
168
+
169
+ end
170
+
171
+ end
172
+
173
+ ```
174
+
175
+
176
+
177
+ >Photos/index.html.erb
178
+
179
+
180
+
181
+ ```Ruby on Rails
182
+
183
+ <div id="tinderslide">
184
+
185
+ <ul>
186
+
187
+ <% @photos.each do |photo| %>
188
+
189
+ <li>
190
+
191
+ <div class="photoCaption"><%= photo.caption %></div>
192
+
193
+ <%= image_tag(photo.image_url) %>
194
+
195
+ <div class="like"></div>
196
+
197
+ <div class="dislike"></div>
198
+
199
+ </li>
200
+
201
+ <% end %>
202
+
203
+ </ul>
204
+
205
+ <div class="noUser">~~</div>
206
+
207
+ </div>
208
+
209
+ ```
210
+
211
+
212
+
17
213
  ### 試したこと
18
214
 
19
215