質問編集履歴
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
タイトル通りなのですがgem'ransack'
|
1
|
+
タイトル通りなのですがgem'ransack'なおかつsearch_form_forを使って検索フォームを作成したいです。
|
2
2
|
|
3
3
|
===
|
4
4
|
|
@@ -8,7 +8,51 @@
|
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
|
11
|
+
<%= search_form_for @q, url: users_path do |f| %>
|
12
|
+
|
13
|
+
<%= f.label :name, "#{t('view.task_name_search')}" %>
|
14
|
+
|
15
|
+
<%= f.search_field :name_cont, id: 'search_field_name' %>
|
16
|
+
|
17
|
+
<%= f.label :state, "#{t('view.task_state_search')}" %>
|
18
|
+
|
19
|
+
<%= f.select :state_eq, ['未着手','着手中','完了'], include_blank: "指定無し" %>
|
20
|
+
|
21
|
+
<%= f.submit "#{t('view.search')}" %>
|
22
|
+
|
23
|
+
<% end %>
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
<!--<div class="cotainer">-->
|
28
|
+
|
29
|
+
<table class="table">
|
30
|
+
|
31
|
+
<thead>
|
32
|
+
|
33
|
+
<tr>
|
34
|
+
|
35
|
+
<th>duty</th>
|
36
|
+
|
37
|
+
<th>Details</th>
|
38
|
+
|
39
|
+
<th>expired_at</th>
|
40
|
+
|
41
|
+
<th>state</th>
|
42
|
+
|
43
|
+
<th>priority</th>
|
44
|
+
|
45
|
+
<th>labels</th>
|
46
|
+
|
47
|
+
<!-- <th colspan="5"></th>-->
|
48
|
+
|
49
|
+
</tr>
|
50
|
+
|
51
|
+
</thead>
|
52
|
+
|
53
|
+
<tbody>
|
54
|
+
|
11
|
-
<% @users.each do |blog| %>
|
55
|
+
<% @users.each do |blog| %>
|
12
56
|
|
13
57
|
<tr>
|
14
58
|
|
@@ -40,6 +84,10 @@
|
|
40
84
|
|
41
85
|
<%= paginate @users %>
|
42
86
|
|
87
|
+
</tbody>
|
88
|
+
|
89
|
+
</table>
|
90
|
+
|
43
91
|
```
|
44
92
|
|
45
93
|
|
@@ -48,27 +96,7 @@
|
|
48
96
|
|
49
97
|
```ruby
|
50
98
|
|
51
|
-
class UsersController < ApplicationController
|
52
|
-
|
53
|
-
before_action :set_user, only: %i[show edit update destroy]
|
54
|
-
|
55
|
-
before_action :only_current_user, only: %i[show edit]
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
# def index
|
60
|
-
|
61
|
-
# @users = User.find(current_user.id).blogs
|
62
|
-
|
63
|
-
# # binding.pry
|
64
|
-
|
65
|
-
# end
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
99
|
+
def index
|
70
|
-
|
71
|
-
@l = Label.ransack(params[:q])
|
72
100
|
|
73
101
|
@q = Blog.ransack(params[:q])
|
74
102
|
|
@@ -78,8 +106,6 @@
|
|
78
106
|
|
79
107
|
@users = @q.result(distinct: true).page(params[:page]).per(7)
|
80
108
|
|
81
|
-
@user = @l.result(distinct: true).page(params[:page]).per(7)
|
82
|
-
|
83
109
|
# binding.pry
|
84
110
|
|
85
111
|
elsif params[:expired_at].present?
|
@@ -102,120 +128,266 @@
|
|
102
128
|
|
103
129
|
end
|
104
130
|
|
105
|
-
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
106
|
-
|
135
|
+
以下のような形で中間テーブルにBlogモデルに紐づいたラベルを登録させています。
|
136
|
+
|
107
|
-
|
137
|
+
```ruby
|
138
|
+
|
108
|
-
|
139
|
+
<% Label.all.each do |label| %>
|
140
|
+
|
141
|
+
<% unless label.user_id == current_user.id %>
|
142
|
+
|
143
|
+
<%= form.check_box :label_ids, { multiple: true, checked: label[:checked], disabled: label[:disabled], include_hidden: false }, label[:id] %>
|
144
|
+
|
145
|
+
<label><%= label.label_name %></label>
|
146
|
+
|
147
|
+
<% end %>
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
schema.rb
|
154
|
+
|
155
|
+
```ruby:schema.rb
|
156
|
+
|
157
|
+
ActiveRecord::Schema.define(version: 2019_08_05_082729) do
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# These are extensions that must be enabled in order to support this database
|
162
|
+
|
163
|
+
enable_extension "plpgsql"
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
create_table "blogs", force: :cascade do |t|
|
168
|
+
|
169
|
+
t.string "name", null: false
|
170
|
+
|
171
|
+
t.text "details", null: false
|
172
|
+
|
173
|
+
t.datetime "created_at", null: false
|
174
|
+
|
175
|
+
t.datetime "updated_at", null: false
|
176
|
+
|
177
|
+
t.datetime "expired_at", default: "2019-08-03 20:25:28", null: false
|
178
|
+
|
179
|
+
t.string "state", default: "完了", null: false
|
180
|
+
|
181
|
+
t.integer "priority", default: 0, null: false
|
182
|
+
|
183
|
+
t.bigint "user_id"
|
184
|
+
|
185
|
+
t.index ["user_id"], name: "index_blogs_on_user_id"
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
create_table "labelings", force: :cascade do |t|
|
192
|
+
|
193
|
+
t.integer "blog_id"
|
194
|
+
|
195
|
+
t.integer "label_id"
|
196
|
+
|
197
|
+
t.datetime "created_at", null: false
|
198
|
+
|
199
|
+
t.datetime "updated_at", null: false
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
create_table "labels", force: :cascade do |t|
|
206
|
+
|
207
|
+
t.string "label_name"
|
208
|
+
|
209
|
+
t.datetime "created_at", null: false
|
210
|
+
|
211
|
+
t.datetime "updated_at", null: false
|
212
|
+
|
213
|
+
t.integer "blog_id"
|
214
|
+
|
215
|
+
t.bigint "user_id"
|
216
|
+
|
217
|
+
t.index ["user_id"], name: "index_labels_on_user_id"
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
create_table "users", force: :cascade do |t|
|
224
|
+
|
225
|
+
t.string "name"
|
226
|
+
|
227
|
+
t.string "email"
|
228
|
+
|
229
|
+
t.datetime "created_at", null: false
|
230
|
+
|
231
|
+
t.datetime "updated_at", null: false
|
232
|
+
|
233
|
+
t.string "password_digest"
|
234
|
+
|
235
|
+
t.boolean "administrator", default: false, null: false
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
add_foreign_key "blogs", "users"
|
242
|
+
|
243
|
+
add_foreign_key "labels", "users"
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
blog.rb
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
|
255
|
+
class Blog < ApplicationRecord
|
256
|
+
|
109
|
-
|
257
|
+
validates :name, presence: true
|
258
|
+
|
110
|
-
|
259
|
+
validates :details, presence: true
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
enum priority:{"低": 0, "中": 1, "高": 2}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
belongs_to :user
|
268
|
+
|
269
|
+
has_many :labelings, dependent: :destroy
|
270
|
+
|
271
|
+
has_many :labels, through: :labelings
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
has_many :task_have_labels, through: :labelings, source: :label
|
276
|
+
|
277
|
+
# accepts_nested_attributes_for :labels, allow_destroy: true
|
278
|
+
|
279
|
+
# reject_if: all_blank
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
label.rb
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
|
291
|
+
class Label < ApplicationRecord
|
292
|
+
|
293
|
+
has_many :labelings, dependent: :destroy
|
294
|
+
|
295
|
+
belongs_to :user, optional: true
|
296
|
+
|
297
|
+
belongs_to :blog, optional: true
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
labeling.rb
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
|
309
|
+
class Labeling < ApplicationRecord
|
310
|
+
|
311
|
+
belongs_to :blog
|
312
|
+
|
313
|
+
belongs_to :label
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
```
|
318
|
+
|
319
|
+
user.rb
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
|
323
|
+
class User < ApplicationRecord
|
324
|
+
|
325
|
+
before_validation { email.downcase! }
|
326
|
+
|
327
|
+
before_destroy :last_one_administrator
|
328
|
+
|
329
|
+
has_many :blogs, dependent: :destroy
|
330
|
+
|
331
|
+
has_many :labels, dependent: :destroy
|
332
|
+
|
333
|
+
validates :name, presence: true, length: { maximum: 30 }
|
334
|
+
|
111
|
-
|
335
|
+
validates :email, presence: true, length: { maximum: 255 }, uniqueness: true,
|
336
|
+
|
337
|
+
format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }
|
338
|
+
|
339
|
+
has_secure_password
|
340
|
+
|
341
|
+
validates :password, presence: true, length: { minimum: 6 }
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
private
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
def last_one_administrator
|
350
|
+
|
351
|
+
admin_user = User.where(administrator: true)
|
352
|
+
|
353
|
+
if admin_user.count == 1 && administrator
|
354
|
+
|
355
|
+
throw :abort
|
356
|
+
|
357
|
+
else
|
112
358
|
|
113
359
|
end
|
114
360
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
en
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
redirect_to user_path(@user)
|
150
|
-
|
151
|
-
else
|
152
|
-
|
153
|
-
render 'new'
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def update
|
162
|
-
|
163
|
-
if @user.update(user_params)
|
164
|
-
|
165
|
-
redirect_to user_path(@user), notice: "更新しました"
|
166
|
-
|
167
|
-
else
|
168
|
-
|
169
|
-
render 'edit'
|
170
|
-
|
171
|
-
end
|
172
|
-
|
173
|
-
end
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
def destroy
|
178
|
-
|
179
|
-
@user.destroy
|
180
|
-
|
181
|
-
redirect_to user_path, notice: "削除しました"
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
private
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
def set_user
|
192
|
-
|
193
|
-
@user = User.find(params[:id])
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
def user_params
|
200
|
-
|
201
|
-
params.require(:user).permit(:name, :email, :password, :password_confirmation)
|
202
|
-
|
203
|
-
end
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
def only_current_user
|
208
|
-
|
209
|
-
if current_user.present?
|
210
|
-
|
211
|
-
else
|
212
|
-
|
213
|
-
redirect_to new_session_path, notice: "ログインしてください"
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
``
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
```
|
366
|
+
|
367
|
+
になります、このindex.html.erbのsearch_form_forにusers_controllerのindexからパラメータを渡しLabelモデルのlabel_nameカラムに登録されている、ラベルで検索フォームを作成したいのですが、うまくいきません。
|
368
|
+
|
369
|
+
以下の検索フォームに追加する形作りたいです。
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
|
373
|
+
<%= search_form_for @q, url: users_path do |f| %>
|
374
|
+
|
375
|
+
<%= f.label :name, "#{t('view.task_name_search')}" %>
|
376
|
+
|
377
|
+
<%= f.search_field :name_cont, id: 'search_field_name' %>
|
378
|
+
|
379
|
+
<%= f.label :state, "#{t('view.task_state_search')}" %>
|
380
|
+
|
381
|
+
<%= f.select :state_eq, ['未着手','着手中','完了'], include_blank: "指定無し" %>
|
382
|
+
|
383
|
+
※ここにラベルを選択ボックスの形で検索できるようにしたいがどのように記述すれば良いかわかりません
|
384
|
+
|
385
|
+
<%= f.submit "#{t('view.search')}" %>
|
386
|
+
|
387
|
+
<% end %>
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
必要な情報は教えていただければその都度あげていきます。
|
392
|
+
|
393
|
+
よろしくお願いします。
|