teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

ビューの全体像を追加

2020/05/01 11:22

投稿

tomohiro2525
tomohiro2525

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,13 @@
1
+ 現在、シェアハウスのマッチングアプリを作成中です。
2
+ イメージとして次のような動きをします。
3
+
4
+ 1募集主が同居の募集する
5
+ 2他者が募集に応募する
6
+ 3募集主が応募に対して、許可をする
7
+
1
8
  データベースに値がある場合trueを返すメソッドの作成について教えていただきたいと思います。
2
9
  find_byやwhereで値を探し出しcountでtrueやfalseを返す形のメソッドを検討しているのですが、どうしても各IDごとで返す方法がわかりませんでした。テーブルごとで返すのではなく、ID個別に値を返す方法がありましたら、ぜひご教示いただければと思います。よろしくお願いいたします。
3
10
 
4
- ok_requestの値を返すメソッドを検討しています。
5
11
  ```MySQL
6
12
  mysql> select * from shares;
7
13
  +----+---------+----------+------------+---------------------+---------------------+
@@ -19,36 +25,78 @@
19
25
  8 rows in set (0.00 sec)
20
26
  ```
21
27
 
28
+ ●user.rb
22
29
  ```Ruby
23
- def permitted_appliments?(house)
30
+ class User < ApplicationRecord
24
-   
31
+ validates :name, presence: true, length: { maximum: 50 }
32
+ validates :email, presence: true, length: { maximum: 255 },
33
+ format: {with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i },
34
+ uniqueness: { case_sensitive: false }
35
+ has_secure_password
36
+
37
+ has_many :houses #募集との紐付け
38
+ has_many :shares #許可テーブルとの紐付け
39
+
40
+
41
+ has_many :requestings, through: :shares, source: :house #リクエストしている募集の取得
42
+ has_many :reverses_of_share, class_name: 'Share', foreign_key: 'ok_request' #自分の募集にリクエストしてくれているユーザーへの参照
43
+
44
+ def request(house) #申請する動き
45
+ self.shares.find_or_create_by(house_id: house.id)
25
46
  end
47
+
48
+ def unrequest(house) #申請を取り消す動き
49
+ request = self.shares.find_by(house_id: house.id)
50
+ request.destroy if request
26
- ```
51
+ end
52
+
53
+ def requesting?(house) #申請してるかの確認
54
+ self.requestings.include?(house)
55
+ end
27
56
 
28
- > たこと→以下のコードで実行しみましたが、うまく動作しませんでした。
57
+ #has_many :ok_requestings, through: :houses, source: :shares #許可している募集の取得
29
- ```Ruby
58
+ has_many :ok_requestings, through: :shares, :foreign_key => 'ok_request', source: :house
59
+
30
- def ok_request?(house) #申請してるかの確認
60
+ def ok_request?(house) #申請を許可してるかの確認
31
61
  self.ok_requestings.include?(house)
32
62
  end
33
63
 
34
- def permitted_appliments?(house)
64
+ def permitted_appliments?(house) #申請を許可してるかの確認
35
65
  return self.shares.where(ok_request: 1)
36
66
  end
37
67
 
38
68
 
39
- def permit_share?(house) #申請してるかの確認
69
+ def permit_share?(house) #申請を許可してるかの確認
40
70
  count = self.shares.where(ok_request: 1).count
41
- return count > 0
71
+ return resurt = count > 0
42
72
  end
43
73
 
44
- def permit_share?(zero="0") #申請してるかの確認
74
+ def permit_share?(zero="0") #申請を許可してるかの確認
45
75
  count = Share.where(ok_request: true).count
46
76
  count > 0 ? count.to_s : zero
47
77
  end
48
78
 
79
+
80
+ has_many :favorites
81
+ has_many :likes, through: :favorites, source: :like
82
+
83
+ def like(house)
84
+ self.favorites.find_or_create_by(like_id: house.id)
85
+ end
86
+
87
+ def unlike(house)
88
+ like = self.favorites.find_by(like_id: house.id)
89
+ like.destroy if like
90
+ end
91
+
92
+ def like?(house)
93
+ self.likes.include?(house)
94
+ end
95
+
96
+ end
49
97
  ```
50
98
 
51
- 許可ボタン
99
+ _ok_button.html.erb(ボタン
52
100
  ```Ruby
53
101
  <% if current_user.permit_share?(house) %>
54
102
  <%= form_with( url: unpermit_share_path, local: true, method: :delete) do |f| %>
@@ -63,4 +111,55 @@
63
111
  <%= f.submit '申請を許可する', class: 'btn btn-primary btn-block' %>
64
112
  <% end %>
65
113
  <% end %>
114
+ ```
115
+
116
+ ●_request_users.html.erb(ボタンをrenderするビューページ)
117
+ ```Ruby
118
+ <% house.shares.each do |share| %>
119
+ <p><%= share.user.name %></p>
120
+ <%= render 'shares/ok_button', user: share.user, house: house %>
121
+ <% end %>
122
+ ```
123
+
124
+ ●shares_controller.rb
125
+ ```Ruby
126
+ class SharesController < ApplicationController
127
+ before_action :require_user_logged_in
128
+
129
+ def create
130
+ house = House.find(params[:house_id])
131
+ current_user.request(house)
132
+ flash[:success] = '同居申請をしました。'
133
+ redirect_to houses_path
134
+ end
135
+
136
+ def destroy
137
+ house = House.find(params[:house_id])
138
+ current_user.unrequest(house)
139
+ flash[:success] = '同居申請を取り消しました'
140
+ redirect_to houses_path
141
+ end
142
+
143
+ def permit_share
144
+ share = Share.where(user_id: params[:user_id])
145
+ .where(house_id: params[:house_id])
146
+ if share
147
+ share.update(ok_request: true)
148
+ flash[:success] = '申請を許可しました。'
149
+ end
150
+ redirect_to house_path(params[:house_id])
151
+ end
152
+
153
+ def unpermit_share
154
+
155
+ share = Share.where(user_id: params[:user_id])
156
+ .where(house_id: params[:house_id])
157
+ if share
158
+ share.update(ok_request: false)
159
+ flash[:success] = '許可を取り消しました。'
160
+ end
161
+ redirect_to house_path(params[:house_id])
162
+ end
163
+
164
+ end
66
165
  ```

4

ボタンのビューの追加

2020/05/01 11:22

投稿

tomohiro2525
tomohiro2525

スコア12

title CHANGED
File without changes
body CHANGED
@@ -46,4 +46,21 @@
46
46
  count > 0 ? count.to_s : zero
47
47
  end
48
48
 
49
+ ```
50
+
51
+ ●許可ボタン
52
+ ```Ruby
53
+ <% if current_user.permit_share?(house) %>
54
+ <%= form_with( url: unpermit_share_path, local: true, method: :delete) do |f| %>
55
+ <%= hidden_field_tag :user_id, user.id %>
56
+ <%= hidden_field_tag :house_id, house.id %>
57
+ <%= f.submit '許可の取り消し', class: 'btn btn-danger btn-block' %>
58
+ <% end %>
59
+ <% else %>
60
+ <%= form_with( url: permit_share_path, local: true) do |f| %>
61
+ <%= hidden_field_tag :user_id, user.id %>
62
+ <%= hidden_field_tag :house_id, house.id %>
63
+ <%= f.submit '申請を許可する', class: 'btn btn-primary btn-block' %>
64
+ <% end %>
65
+ <% end %>
49
66
  ```

3

MySQLの訂正

2020/04/30 10:23

投稿

tomohiro2525
tomohiro2525

スコア12

title CHANGED
File without changes
body CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  ok_requestの値を返すメソッドを検討しています。
5
5
  ```MySQL
6
+ mysql> select * from shares;
6
7
  +----+---------+----------+------------+---------------------+---------------------+
7
8
  | id | user_id | house_id | ok_request | created_at | updated_at |
8
9
  +----+---------+----------+------------+---------------------+---------------------+
@@ -15,6 +16,7 @@
15
16
  | 13 | 3 | 3 | 0 | 2020-04-24 14:47:55 | 2020-04-24 14:47:55 |
16
17
  | 15 | 1 | 5 | 0 | 2020-04-28 10:50:48 | 2020-04-28 10:50:48 |
17
18
  +----+---------+----------+------------+---------------------+---------------------+
19
+ 8 rows in set (0.00 sec)
18
20
  ```
19
21
 
20
22
  ```Ruby

2

試したことの追記

2020/04/30 07:41

投稿

tomohiro2525
tomohiro2525

スコア12

title CHANGED
File without changes
body CHANGED
@@ -21,4 +21,27 @@
21
21
  def permitted_appliments?(house)
22
22
    
23
23
  end
24
+ ```
25
+
26
+ > 試したこと→以下のコードで実行してみましたが、うまく動作しませんでした。
27
+ ```Ruby
28
+ def ok_request?(house) #申請してるかの確認
29
+ self.ok_requestings.include?(house)
30
+ end
31
+
32
+ def permitted_appliments?(house)
33
+ return self.shares.where(ok_request: 1)
34
+ end
35
+
36
+
37
+ def permit_share?(house) #申請してるかの確認
38
+ count = self.shares.where(ok_request: 1).count
39
+ return count > 0
40
+ end
41
+
42
+ def permit_share?(zero="0") #申請してるかの確認
43
+ count = Share.where(ok_request: true).count
44
+ count > 0 ? count.to_s : zero
45
+ end
46
+
24
47
  ```

1

Rubyのコードを記述

2020/04/30 07:38

投稿

tomohiro2525
tomohiro2525

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,7 @@
1
1
  データベースに値がある場合trueを返すメソッドの作成について教えていただきたいと思います。
2
2
  find_byやwhereで値を探し出しcountでtrueやfalseを返す形のメソッドを検討しているのですが、どうしても各IDごとで返す方法がわかりませんでした。テーブルごとで返すのではなく、ID個別に値を返す方法がありましたら、ぜひご教示いただければと思います。よろしくお願いいたします。
3
3
 
4
+ ok_requestの値を返すメソッドを検討しています。
4
5
  ```MySQL
5
6
  +----+---------+----------+------------+---------------------+---------------------+
6
7
  | id | user_id | house_id | ok_request | created_at | updated_at |
@@ -14,4 +15,10 @@
14
15
  | 13 | 3 | 3 | 0 | 2020-04-24 14:47:55 | 2020-04-24 14:47:55 |
15
16
  | 15 | 1 | 5 | 0 | 2020-04-28 10:50:48 | 2020-04-28 10:50:48 |
16
17
  +----+---------+----------+------------+---------------------+---------------------+
18
+ ```
19
+
20
+ ```Ruby
21
+ def permitted_appliments?(house)
22
+   
23
+ end
17
24
  ```