質問編集履歴
3
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -76,6 +76,8 @@
|
|
76
76
|
|
77
77
|
コントローラー
|
78
78
|
|
79
|
+
users_controller.rb
|
80
|
+
|
79
81
|
```Rails
|
80
82
|
|
81
83
|
class UsersController < ApplicationController
|
@@ -326,6 +328,8 @@
|
|
326
328
|
|
327
329
|
パーシャル
|
328
330
|
|
331
|
+
_randomtags.html
|
332
|
+
|
329
333
|
```_randomtags.html.erb
|
330
334
|
|
331
335
|
<% @randomtags.each do |randomtag| %>
|
@@ -344,6 +348,8 @@
|
|
344
348
|
|
345
349
|
ルーティング
|
346
350
|
|
351
|
+
routes.rb
|
352
|
+
|
347
353
|
```ここに言語を入力
|
348
354
|
|
349
355
|
Rails.application.routes.draw do
|
2
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -74,6 +74,8 @@
|
|
74
74
|
|
75
75
|
|
76
76
|
|
77
|
+
コントローラー
|
78
|
+
|
77
79
|
```Rails
|
78
80
|
|
79
81
|
class UsersController < ApplicationController
|
@@ -128,7 +130,89 @@
|
|
128
130
|
|
129
131
|
end
|
130
132
|
|
133
|
+
|
134
|
+
|
135
|
+
def show
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def new
|
142
|
+
|
143
|
+
@user = User.new
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def create
|
150
|
+
|
151
|
+
@user = User.new(user_params)
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
if @user.save
|
156
|
+
|
157
|
+
flash[:success] = 'ユーザを登録しました。'
|
158
|
+
|
159
|
+
redirect_to @user
|
160
|
+
|
161
|
+
else
|
162
|
+
|
163
|
+
flash.now[:danger] = 'ユーザの登録に失敗しました。'
|
164
|
+
|
165
|
+
render :new
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
def edit
|
174
|
+
|
175
|
+
@user = User.find(params[:id])
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def update
|
182
|
+
|
183
|
+
@user = User.find(params[:id])
|
184
|
+
|
185
|
+
if @user.update_attributes(user_params)
|
186
|
+
|
187
|
+
flash[:success] = "Profile updated"
|
188
|
+
|
189
|
+
redirect_to @user
|
190
|
+
|
191
|
+
else
|
192
|
+
|
193
|
+
render 'edit'
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def user_params
|
206
|
+
|
207
|
+
params.require(:user).permit(:name, :email, :password, :password_confirmation)
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
131
|
-
```
|
213
|
+
```
|
214
|
+
|
215
|
+
|
132
216
|
|
133
217
|
|
134
218
|
|
@@ -138,7 +222,9 @@
|
|
138
222
|
|
139
223
|
パーシャルへレンダーしています。
|
140
224
|
|
141
|
-
|
225
|
+
index.js.erb
|
226
|
+
|
227
|
+
```
|
142
228
|
|
143
229
|
$('#samplRemote').html("<%= escape_javascript(render 'randomtags') %>");
|
144
230
|
|
@@ -146,7 +232,9 @@
|
|
146
232
|
|
147
233
|
|
148
234
|
|
149
|
-
|
235
|
+
index.html.erb
|
236
|
+
|
237
|
+
```
|
150
238
|
|
151
239
|
<div id="samplRemote">
|
152
240
|
|
@@ -186,6 +274,116 @@
|
|
186
274
|
|
187
275
|
<!-- Trigger -->
|
188
276
|
|
277
|
+
|
278
|
+
|
279
|
+
<div class="mytags">
|
280
|
+
|
281
|
+
<div class="mytag">
|
282
|
+
|
283
|
+
<h3>My Tag</h3>
|
284
|
+
|
285
|
+
</div>
|
286
|
+
|
287
|
+
<div class="mytag">
|
288
|
+
|
289
|
+
<%= form_with(model: @hashtag, remote: true) do |f| %>
|
290
|
+
|
291
|
+
<div class = "addtag">
|
292
|
+
|
293
|
+
<%= f.submit 'Add Tag'%>
|
294
|
+
|
295
|
+
</div>
|
296
|
+
|
297
|
+
<!--<div class="form-group">-->
|
298
|
+
|
299
|
+
<div class = "textarea">
|
300
|
+
|
301
|
+
<%= f.text_area :content, rows: 1 %>
|
302
|
+
|
303
|
+
</div>
|
304
|
+
|
305
|
+
<!--</div>-->
|
306
|
+
|
307
|
+
<% end %>
|
308
|
+
|
309
|
+
</div>
|
310
|
+
|
311
|
+
</div>
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
<div class="box3">
|
316
|
+
|
317
|
+
<%= render 'hashtags/hashtags', hashtags: @hashtags %>
|
318
|
+
|
319
|
+
</div>
|
320
|
+
|
189
321
|
</div>
|
190
322
|
|
323
|
+
|
324
|
+
|
191
|
-
```
|
325
|
+
```
|
326
|
+
|
327
|
+
パーシャル
|
328
|
+
|
329
|
+
```_randomtags.html.erb
|
330
|
+
|
331
|
+
<% @randomtags.each do |randomtag| %>
|
332
|
+
|
333
|
+
<div class = "r">
|
334
|
+
|
335
|
+
<span>#<%= randomtag.content %></span>
|
336
|
+
|
337
|
+
</div>
|
338
|
+
|
339
|
+
<% end %>
|
340
|
+
|
341
|
+
```
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
ルーティング
|
346
|
+
|
347
|
+
```ここに言語を入力
|
348
|
+
|
349
|
+
Rails.application.routes.draw do
|
350
|
+
|
351
|
+
get 'hashtags/create'
|
352
|
+
|
353
|
+
get 'hashtags/destroy'
|
354
|
+
|
355
|
+
get 'sessions/new'
|
356
|
+
|
357
|
+
get 'sessions/create'
|
358
|
+
|
359
|
+
get 'sessions/destroy'
|
360
|
+
|
361
|
+
get 'hashtags/index'
|
362
|
+
|
363
|
+
post 'hashtags/index'
|
364
|
+
|
365
|
+
root to: 'toppages#index'
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
get 'login', to: 'sessions#new'
|
370
|
+
|
371
|
+
post 'login', to: 'sessions#create'
|
372
|
+
|
373
|
+
delete 'logout', to: 'sessions#destroy'
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
get 'signup', to: 'users#new'
|
378
|
+
|
379
|
+
resources :users, only: [:index, :show, :new, :create, :edit, :update]
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
resources :hashtags, only: [:create, :destroy, :show]
|
386
|
+
|
387
|
+
end
|
388
|
+
|
389
|
+
```
|
1
コントローラーなど
test
CHANGED
File without changes
|
test
CHANGED
@@ -51,3 +51,141 @@
|
|
51
51
|
<% end %>
|
52
52
|
|
53
53
|
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
追記
|
60
|
+
|
61
|
+
ありがとうございます。
|
62
|
+
|
63
|
+
確かにlocal: trueを削除するとJSに切り替わったようです。
|
64
|
+
|
65
|
+
ですがボタンクリックしても作動しません。
|
66
|
+
|
67
|
+
これはコントローラー側のコードが悪いのでしょうか?
|
68
|
+
|
69
|
+
ここを参考にしながら書きました。
|
70
|
+
|
71
|
+
https://www.sejuku.net/blog/28967
|
72
|
+
|
73
|
+
http://song-of-life.hatenablog.com/entry/2017/09/30/234356
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```Rails
|
78
|
+
|
79
|
+
class UsersController < ApplicationController
|
80
|
+
|
81
|
+
before_action :require_user_logged_in, only: [:index, :show]
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def index
|
86
|
+
|
87
|
+
if logged_in?
|
88
|
+
|
89
|
+
@hashtag = current_user.hashtags.build # form_with 用
|
90
|
+
|
91
|
+
@hashtags = current_user.hashtags.order(id: :desc).page(params[:page])
|
92
|
+
|
93
|
+
if params[:hashtag].nil?
|
94
|
+
|
95
|
+
@randomtags = Hashtag.order("RAND()").limit(28)
|
96
|
+
|
97
|
+
# Ajax
|
98
|
+
|
99
|
+
respond_to do |format|
|
100
|
+
|
101
|
+
format.html
|
102
|
+
|
103
|
+
format.js
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
# Ajax
|
108
|
+
|
109
|
+
else
|
110
|
+
|
111
|
+
@randomtags = Hashtag.order("RAND()").limit(params[:hashtag][:hashtags])
|
112
|
+
|
113
|
+
# Ajax
|
114
|
+
|
115
|
+
respond_to do |format|
|
116
|
+
|
117
|
+
format.html
|
118
|
+
|
119
|
+
format.js
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
# Ajax
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
ビューファイルです。
|
138
|
+
|
139
|
+
パーシャルへレンダーしています。
|
140
|
+
|
141
|
+
```index.js.erb
|
142
|
+
|
143
|
+
$('#samplRemote').html("<%= escape_javascript(render 'randomtags') %>");
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```index.html.erb
|
150
|
+
|
151
|
+
<div id="samplRemote">
|
152
|
+
|
153
|
+
<%= form_with(model: @hashtag, url: users_path, method: :get, remote: true) do |f| %>
|
154
|
+
|
155
|
+
<%= f.submit 'RandomTag', class: 'btn1 btn-primary btn-sm' %>
|
156
|
+
|
157
|
+
<!--<div>-->
|
158
|
+
|
159
|
+
<button class="clipboard-btn btn1 btn-primary btn-sm" data-clipboard-action="copy" data-clipboard-target="#bar">
|
160
|
+
|
161
|
+
Copy
|
162
|
+
|
163
|
+
</button>
|
164
|
+
|
165
|
+
<!--</div>-->
|
166
|
+
|
167
|
+
<div class = "setnumber">
|
168
|
+
|
169
|
+
<%= f.label :"Set Number" %>
|
170
|
+
|
171
|
+
</div>
|
172
|
+
|
173
|
+
<%= f.select :hashtags, 1.upto(30).map{|i| [i, i]}, class:"form-control" %><br>
|
174
|
+
|
175
|
+
<% end %>
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
<!-- Target -->
|
180
|
+
|
181
|
+
<div class="box2" id="bar">
|
182
|
+
|
183
|
+
<p><%= render 'randomtags/randomtags', randomtags: @randomtags %></p>
|
184
|
+
|
185
|
+
</div>
|
186
|
+
|
187
|
+
<!-- Trigger -->
|
188
|
+
|
189
|
+
</div>
|
190
|
+
|
191
|
+
```
|