質問編集履歴
6
読みやすく修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
Ruby on Railsで写真とテキストを投稿できるSNSを作っています。
|
2
2
|
|
3
3
|
検索機能を実装したいのですがうまく動きません。
|
4
|
-
具体的には`ALL`,`
|
4
|
+
具体的には`ALL`,`MEN`, `WOMEN`の3つのボタンを作り、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、`WOMEN`の時には女性のレコードを表示する機能です。
|
5
5
|
|
6
6
|
現在のコードでは、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、なぜか`WOMEN`の時にも男性のレコードが表示されてしまいます。
|
7
7
|
|
8
8
|
ユーザーは`usersテーブル`という名前で、投稿は`postsテーブル`という名前を使っていて、ユーザーは`devise`を使っています。
|
9
|
+
`gender_id`は`Active Hash`を使っています。
|
9
|
-
`user`と`post`は、
|
10
|
+
`user`と`post`は、`1:N`の関係です。
|
10
11
|
検索のために`searchアクション`を定義しています。
|
11
12
|
`search.html.erb`は`index.htl.erb`と同じコードにしています。
|
12
13
|
|
5
本文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
検索機能を実装したいのですがうまく動きません。
|
4
4
|
具体的には`ALL`,` MEN`, `WOMEN`の3つのボタンを作り、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、`WOMEN`の時には女性のレコードを表示する機能です。
|
5
5
|
|
6
|
-
現在のコードでは、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、`WOMEN`の時にも男性のレコードが表示されてしまいます。
|
6
|
+
現在のコードでは、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、なぜか`WOMEN`の時にも男性のレコードが表示されてしまいます。
|
7
7
|
|
8
8
|
ユーザーは`usersテーブル`という名前で、投稿は`postsテーブル`という名前を使っていて、ユーザーは`devise`を使っています。
|
9
9
|
`user`と`post`は、「1:N」の関係です。
|
4
わかりやすく文章とコードを編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,36 +1,43 @@
|
|
1
|
-
#Ruby on Rails
|
2
|
-
一覧ページにある`link_to`を押した時に、子モデル(postモデル)のレコードを、親モデル(userモデル)のカラムである性別(gender_id)で検索する方法を教えていただきたいです。
|
3
|
-
|
1
|
+
Ruby on Railsで写真とテキストを投稿できるSNSを作っています。
|
4
2
|
|
3
|
+
検索機能を実装したいのですがうまく動きません。
|
5
|
-
|
4
|
+
具体的には`ALL`,` MEN`, `WOMEN`の3つのボタンを作り、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、`WOMEN`の時には女性のレコードを表示する機能です。
|
6
5
|
|
6
|
+
現在のコードでは、`ALL`の時には全てのレコード、`MEN`の時には男性のレコード、`WOMEN`の時にも男性のレコードが表示されてしまいます。
|
7
|
+
|
8
|
+
ユーザーは`usersテーブル`という名前で、投稿は`postsテーブル`という名前を使っていて、ユーザーは`devise`を使っています。
|
9
|
+
`user`と`post`は、「1:N」の関係です。
|
10
|
+
検索のために`searchアクション`を定義しています。
|
11
|
+
`search.html.erb`は`index.htl.erb`と同じコードにしています。
|
12
|
+
|
13
|
+
|
14
|
+
###ソースコード
|
7
|
-
```
|
15
|
+
```controller
|
16
|
+
-----省略
|
17
|
+
|
18
|
+
def search
|
8
|
-
|
19
|
+
@posts = Post.search(params[:gender_id])
|
20
|
+
end
|
21
|
+
|
9
|
-
|
22
|
+
-----省略
|
10
|
-
<%= link_to "ALL", root_path, class:"gender" %>
|
11
|
-
</li>
|
12
|
-
<li>
|
13
|
-
<%= link_to "MEN", search_posts_path(gender_id:2), class:"gender" %>
|
14
|
-
</li>
|
15
|
-
<li>
|
16
|
-
<%= link_to "WOMEN", search_posts_path(gender_id:3), class:"gender" %>
|
17
|
-
</li>
|
18
|
-
</ul>
|
19
23
|
```
|
20
24
|
|
21
|
-
```
|
25
|
+
```routes
|
26
|
+
Rails.application.routes.draw do
|
27
|
+
devise_for :users
|
28
|
+
root "posts#index"
|
29
|
+
resources :posts, only: [:new, :create, :show, :destroy, :edit, :update] do
|
22
|
-
|
30
|
+
resources :comments, only: :create
|
23
|
-
|
31
|
+
collection do
|
24
|
-
{ id: 1, name: '---' },
|
25
|
-
{ id: 2, name: 'MEN' },
|
26
|
-
{ id: 3, name: 'WOMEN' },
|
27
|
-
|
32
|
+
get "search"
|
28
|
-
|
33
|
+
end
|
29
34
|
end
|
30
|
-
|
35
|
+
resources :users, only: :show
|
36
|
+
end
|
37
|
+
|
31
38
|
```
|
32
39
|
|
33
|
-
```
|
40
|
+
```model
|
34
41
|
class Post < ApplicationRecord
|
35
42
|
has_many_attached :images
|
36
43
|
has_many :comments, dependent: :destroy
|
@@ -45,135 +52,30 @@
|
|
45
52
|
validates :text, length: { maximum: 150, message: "は150文字以下にしてください" }
|
46
53
|
|
47
54
|
def self.search(search)
|
48
|
-
if search =
|
55
|
+
if search = 2
|
49
56
|
Post.joins(:user).where("users.gender_id = 2")
|
50
|
-
elsif search =
|
57
|
+
elsif search = 3
|
51
58
|
Post.joins(:user).where("users.gender_id = 3")
|
52
|
-
else
|
53
|
-
Post.includes(:user).order("created_at DESC")
|
54
59
|
end
|
55
60
|
end
|
56
61
|
end
|
57
62
|
|
58
63
|
```
|
59
64
|
|
60
|
-
```
|
65
|
+
```indexhtmlerb
|
61
|
-
|
66
|
+
-------省略
|
62
|
-
# Include default devise modules. Others available are:
|
63
|
-
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
64
|
-
devise :database_authenticatable, :registerable,
|
65
|
-
:recoverable, :rememberable, :validatable
|
66
67
|
|
68
|
+
<ul class="gender-lists">
|
69
|
+
<li>
|
67
|
-
|
70
|
+
<%= link_to "ALL", root_path, class:"gender" %>
|
68
|
-
|
71
|
+
</li>
|
72
|
+
<li>
|
69
|
-
|
73
|
+
<%= link_to "MEN", search_posts_path(gender_id:2), class:"gender" %>
|
74
|
+
</li>
|
75
|
+
<li>
|
70
|
-
|
76
|
+
<%= link_to "WOMEN", search_posts_path(gender_id:3), class:"gender" %>
|
77
|
+
</li>
|
78
|
+
</ul>
|
71
79
|
|
72
|
-
with_options presence: true do
|
73
|
-
validates :nickname
|
74
|
-
validates :gender_id
|
75
|
-
end
|
76
|
-
|
77
|
-
validates :email, uniqueness: true
|
78
|
-
|
79
|
-
validates :nickname, length: { maximum: 8, message: "は8文字以下にしてください" }
|
80
|
-
|
81
|
-
validates :gender_id, numericality: { other_than: 1, message: "を選択してください" }
|
82
|
-
|
83
|
-
half_width_alphanumeric = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,100}+\z/i
|
84
|
-
validates :password, presence: true, format: { with: half_width_alphanumeric, message: "に半角英数字を使用してください" }
|
85
|
-
end
|
86
|
-
|
87
|
-
```
|
88
|
-
|
89
|
-
```postcontroller
|
90
|
-
class PostsController < ApplicationController
|
91
|
-
before_action :set_post, only: [:show, :destroy, :edit, :update]
|
92
|
-
before_action :correct_user, only: [:edit, :update]
|
93
|
-
|
94
|
-
|
80
|
+
-------省略
|
95
|
-
@posts = Post.includes(:user).order("created_at DESC")
|
96
|
-
end
|
97
|
-
|
98
|
-
def new
|
99
|
-
@post = Post.new
|
100
|
-
end
|
101
|
-
|
102
|
-
def create
|
103
|
-
@post = Post.new(post_params)
|
104
|
-
if @post.valid?
|
105
|
-
@post.save
|
106
|
-
flash[:notice] = "投稿が完了しました"
|
107
|
-
redirect_to root_path
|
108
|
-
else
|
109
|
-
flash.now[:alert] = "投稿に失敗しました"
|
110
|
-
render :new
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def show
|
115
|
-
@comment = Comment.new
|
116
|
-
@comments = @post.comments.includes(:user)
|
117
|
-
end
|
118
|
-
|
119
|
-
def destroy
|
120
|
-
if @post.destroy
|
121
|
-
flash[:notice] = "削除が完了しました"
|
122
|
-
redirect_to root_path
|
123
|
-
else
|
124
|
-
flash.now[:alert] = "削除に失敗しました"
|
125
|
-
render :show
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def edit
|
130
|
-
end
|
131
|
-
|
132
|
-
def update
|
133
|
-
if @post.update(post_params)
|
134
|
-
flash[:notice] = "編集が完了しました"
|
135
|
-
redirect_to post_path(@post.id)
|
136
|
-
else
|
137
|
-
flash.now[:alert] = "編集に失敗しました"
|
138
|
-
render :edit
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def search
|
143
|
-
@posts = Post.search(params[:gender_id])
|
144
|
-
end
|
145
|
-
|
146
|
-
private
|
147
|
-
|
148
|
-
def post_params
|
149
|
-
params.require(:post).permit(:text, images: []).merge(user_id: current_user.id)
|
150
|
-
end
|
151
|
-
|
152
|
-
def set_post
|
153
|
-
@post = Post.find(params[:id])
|
154
|
-
end
|
155
|
-
|
156
|
-
def correct_user
|
157
|
-
unless user_signed_in? && current_user.id == @post.user.id
|
158
|
-
redirect_to root_path
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
```
|
164
|
-
.
|
165
|
-
.
|
166
|
-
.
|
167
|
-
|
168
|
-
--------------追記---------------------
|
169
|
-
`postmodel`の`self.search`の記述を下記のように`search == 2`から `search = 2`のように書き換えたところ、
|
170
|
-
`MEN`のボタンを押したときは男性だけの投稿になりましたが、`WOMEN`のボタンを押しても男性の投稿になってしまいます。
|
171
|
-
|
172
|
-
```postmodel
|
173
|
-
def self.search(search)
|
174
|
-
if search = 2
|
175
|
-
Post.joins(:user).where("users.gender_id = 2")
|
176
|
-
elsif search = 3
|
177
|
-
Post.joins(:user).where("users.gender_id = 3")
|
178
|
-
end
|
179
81
|
```
|
3
誤字の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -167,7 +167,7 @@
|
|
167
167
|
|
168
168
|
--------------追記---------------------
|
169
169
|
`postmodel`の`self.search`の記述を下記のように`search == 2`から `search = 2`のように書き換えたところ、
|
170
|
-
`MEN`のボタンを押したときは男性だけの投稿になりましたが、`
|
170
|
+
`MEN`のボタンを押したときは男性だけの投稿になりましたが、`WOMEN`のボタンを押しても男性の投稿になってしまいます。
|
171
171
|
|
172
172
|
```postmodel
|
173
173
|
def self.search(search)
|
2
情報の更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -160,4 +160,20 @@
|
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
|
+
```
|
164
|
+
.
|
165
|
+
.
|
166
|
+
.
|
167
|
+
|
168
|
+
--------------追記---------------------
|
169
|
+
`postmodel`の`self.search`の記述を下記のように`search == 2`から `search = 2`のように書き換えたところ、
|
170
|
+
`MEN`のボタンを押したときは男性だけの投稿になりましたが、`WEMEN`のボタンを押しても男性の投稿になってしまいます。
|
171
|
+
|
172
|
+
```postmodel
|
173
|
+
def self.search(search)
|
174
|
+
if search = 2
|
175
|
+
Post.joins(:user).where("users.gender_id = 2")
|
176
|
+
elsif search = 3
|
177
|
+
Post.joins(:user).where("users.gender_id = 3")
|
178
|
+
end
|
163
179
|
```
|
1
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,9 +1,23 @@
|
|
1
1
|
#Ruby on Rails
|
2
|
-
子モデル(postモデル)のレコードを、親モデル(userモデル)のカラムである性別(gender_id)で検索する方法を教えていただきたいです。
|
2
|
+
一覧ページにある`link_to`を押した時に、子モデル(postモデル)のレコードを、親モデル(userモデル)のカラムである性別(gender_id)で検索する方法を教えていただきたいです。
|
3
3
|
gender_idはAcrtive Hashを使っています。
|
4
4
|
|
5
5
|
検索のための`search`アクションを作成し、`post`モデルに`joins`と`where`を使って検索のコードを記述をしましたが、上手くいかず、原因がわかりません。
|
6
6
|
|
7
|
+
```indexhtmlerb
|
8
|
+
<ul class="gender-lists">
|
9
|
+
<li>
|
10
|
+
<%= link_to "ALL", root_path, class:"gender" %>
|
11
|
+
</li>
|
12
|
+
<li>
|
13
|
+
<%= link_to "MEN", search_posts_path(gender_id:2), class:"gender" %>
|
14
|
+
</li>
|
15
|
+
<li>
|
16
|
+
<%= link_to "WOMEN", search_posts_path(gender_id:3), class:"gender" %>
|
17
|
+
</li>
|
18
|
+
</ul>
|
19
|
+
```
|
20
|
+
|
7
21
|
```gendermodel
|
8
22
|
class Gender < ActiveHash::Base
|
9
23
|
self.data = [
|