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

質問編集履歴

3

Listscontrollerを記述

2020/01/02 03:10

投稿

DaikiUTT
DaikiUTT

スコア5

title CHANGED
File without changes
body CHANGED
@@ -162,8 +162,93 @@
162
162
  end
163
163
  ```
164
164
 
165
+ ```Ruby
166
+ class ListsController < ApplicationController
167
+ before_action :set_list, only: [:show, :edit, :update, :destroy]
165
168
 
169
+ # GET /lists
170
+ # GET /lists.json
171
+ def index
166
172
 
173
+ #@list = List.find(params[:id])
174
+
175
+ @lists= List.where(user_id: current_user.id).order("start_time ASC")
176
+ end
177
+
178
+ # GET /lists/1
179
+ # GET /lists/1.json
180
+ def show
181
+ end
182
+
183
+ # GET /lists/new
184
+ def new
185
+ @list = List.new
186
+ end
187
+
188
+ # GET /lists/1/edit
189
+ def edit
190
+ end
191
+
192
+ # POST /lists
193
+ # POST /lists.json
194
+ def create
195
+ @list = List.new(list_params)
196
+
197
+ @list.user_id = current_user.id
198
+
199
+ #@list.tag_list.user_id = current_user.id
200
+
201
+
202
+ respond_to do |format|
203
+ if @list.save
204
+ format.html { redirect_to @list, notice: 'List was successfully created.' }
205
+ format.json { render :show, status: :created, location: @list }
206
+ else
207
+ format.html { render :new }
208
+ format.json { render json: @list.errors, status: :unprocessable_entity }
209
+ end
210
+ end
211
+ end
212
+
213
+ # PATCH/PUT /lists/1
214
+ # PATCH/PUT /lists/1.json
215
+ def update
216
+ respond_to do |format|
217
+ if @list.update(list_params)
218
+ format.html { redirect_to @list, notice: 'List was successfully updated.' }
219
+ format.json { render :show, status: :ok, location: @list }
220
+ else
221
+ format.html { render :edit }
222
+ format.json { render json: @list.errors, status: :unprocessable_entity }
223
+ end
224
+ end
225
+ end
226
+
227
+ # DELETE /lists/1
228
+ # DELETE /lists/1.json
229
+ def destroy
230
+ @list.destroy
231
+ respond_to do |format|
232
+ format.html { redirect_to tag_lists_url, notice: 'List was successfully destroyed.' }
233
+ format.json { head :no_content }
234
+ end
235
+ end
236
+
237
+ private
238
+ # Use callbacks to share common setup or constraints between actions.
239
+ def set_list
240
+ @list = List.find(params[:id])
241
+ end
242
+
243
+ # Never trust parameters from the scary internet, only allow the white list through.
244
+ def list_params
245
+ params.require(:list).permit(:content,:start_time, tag_ids:[])
246
+ end
247
+ end
248
+ ```
249
+
250
+
251
+
167
252
  ### 補足情報(FW/ツールのバージョンなど)
168
253
 
169
254
  rails 5.1.7

2

タイトル

2020/01/02 03:10

投稿

DaikiUTT
DaikiUTT

スコア5

title CHANGED
@@ -1,1 +1,1 @@
1
- Railsで、中間テーブルのカラムに外部のテーブルからカラムの値を参照したい
1
+ Railsで、中間テーブルのカラムにのテーブルからカラムの値を参照したい
body CHANGED
File without changes

1

質問文のコード部分の書き方

2019/12/29 18:10

投稿

DaikiUTT
DaikiUTT

スコア5

title CHANGED
File without changes
body CHANGED
@@ -1,28 +1,169 @@
1
1
  ### 前提・実現したいこと
2
- ```
3
2
 
3
+
4
4
  Tagモデル、ListモデルをTagListモデルを中間テーブルとして関連付けしています。
5
5
  ログイン中のユーザーの作成したもののみをtag_lists_controllerで@tag_lists = TagList.where(user_id: current_user.id)とすることでtaglistsの一覧ページで表示させたいのですが、
6
6
  コマンドラインで確認したところTagList.user_idがnilになっているためうまくできません。
7
+ なので、今回実現したいことはTagList.user_idにcurrent_user.idを代入できるようにすることです。
7
8
 
9
+ 各テーブルのカラムは以下のようになっています。
10
+ ```Ruby
11
+ create_table "tags", force: :cascade do |t|
12
+ t.string "content"
13
+ t.datetime "created_at", null: false
14
+ t.datetime "updated_at", null: false
15
+ t.integer "user_id"
16
+ end
8
17
 
18
+ create_table "lists", force: :cascade do |t|
19
+ t.string "content"
20
+ t.datetime "created_at", null: false
21
+ t.datetime "updated_at", null: false
22
+ t.integer "user_id"
23
+ t.date "start_time"
24
+ end
25
+
26
+ create_table "tag_lists", force: :cascade do |t|
27
+ t.integer "tag_id"
28
+ t.integer "list_id"
29
+ t.datetime "created_at", null: false
30
+ t.datetime "updated_at", null: false
31
+ t.integer "user_id"
32
+ t.index ["list_id"], name: "index_tag_lists_on_list_id"
33
+ t.index ["tag_id"], name: "index_tag_lists_on_tag_id"
34
+ end
35
+
36
+ create_table "users", force: :cascade do |t|
37
+ t.string "email", default: "", null: false
38
+ t.string "encrypted_password", default: "", null: false
39
+ t.string "reset_password_token"
40
+ t.datetime "reset_password_sent_at"
41
+ t.datetime "remember_created_at"
42
+ t.datetime "created_at", null: false
43
+ t.datetime "updated_at", null: false
44
+ t.index ["email"], name: "index_users_on_email", unique: true
45
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
46
+ end
47
+
48
+ ```
49
+
50
+ また、各モデルのアソシエーションは以下のようになっています。
51
+ ```Ruby
52
+ class Tag < ApplicationRecord
53
+ belongs_to :user, optional: true
54
+ has_many :tag_lists, dependent: :destroy
55
+ has_many :lists, :through => :tag_lists
56
+ end
57
+
58
+ class List < ApplicationRecord
59
+ belongs_to :user, optional: true
60
+ has_many :tag_lists, dependent: :destroy
61
+ has_many :tags, :through => :tag_lists
62
+ end
63
+
64
+ class TagList < ApplicationRecord
65
+ belongs_to :user, optional: true
66
+ belongs_to :tag, optional: true
67
+ belongs_to :list, optional: true
68
+ end
69
+
70
+ class User < ApplicationRecord
71
+ has_many :diaries
72
+ has_many :tags
73
+ has_many :lists
74
+ has_many :tag_lists
75
+ end
76
+ ```
77
+
78
+
9
79
  ### 発生している問題・エラーメッセージ
80
+ エラーメッセージは表示されていません。
10
81
  コマンドラインでrails cをしてTagList.allと入力するとuser_id: nilと表示されます。
11
82
  本来はこのnilの部分にcurrent_user.id(ログイン中のユーザーのid)が入るようにしたいです。
12
- ```
13
- エラーメッセージ
14
- ```
15
83
 
84
+
16
85
  ### 該当のソースコード
17
86
 
18
- ```ここに言語名を入力
87
+ ```Ruby
88
+ class TagListsController < ApplicationController
89
+ before_action :set_tag_list, only: [:show, :edit, :update, :destroy]
90
+
91
+ def index
92
+ @tag_lists = TagList.where(user_id: current_user.id)
93
+ end
94
+
95
+ # GET /tag_lists/1
96
+ # GET /tag_lists/1.json
97
+ def show
98
+ end
99
+
100
+ # GET /tag_lists/new
19
- ソースコード
101
+ def new
102
+ @tag_list = TagList.new
103
+ end
104
+
105
+ # GET /tag_lists/1/edit
106
+ def edit
107
+ end
108
+
109
+ # POST /tag_lists
110
+ # POST /tag_lists.json
111
+ def create
112
+ @tag_list = TagList.new(tag_list_params)
113
+
114
+ @tag_list.user_id = current_user.id
115
+ #@tag_list.user_id = tag_list.list.user_id
116
+
117
+ respond_to do |format|
118
+ if @tag_list.save
119
+ format.html { redirect_to @tag_list, notice: 'Tag list was successfully created.' }
120
+ format.json { render :show, status: :created, location: @tag_list }
121
+ else
122
+ format.html { render :new }
123
+ format.json { render json: @tag_list.errors, status: :unprocessable_entity }
124
+ end
125
+ end
126
+ end
127
+
128
+ # PATCH/PUT /tag_lists/1
129
+ # PATCH/PUT /tag_lists/1.json
130
+ def update
131
+ respond_to do |format|
132
+ if @tag_list.update(tag_list_params)
133
+ format.html { redirect_to @tag_list, notice: 'Tag list was successfully updated.' }
134
+ format.json { render :show, status: :ok, location: @tag_list }
135
+ else
136
+ format.html { render :edit }
137
+ format.json { render json: @tag_list.errors, status: :unprocessable_entity }
138
+ end
139
+ end
140
+ end
141
+
142
+ # DELETE /tag_lists/1
143
+ # DELETE /tag_lists/1.json
144
+ def destroy
145
+ @tag_list.destroy
146
+ respond_to do |format|
147
+ format.html { redirect_to tag_lists_url, notice: 'Tag list was successfully destroyed.' }
148
+ format.json { head :no_content }
149
+ end
150
+ end
151
+
152
+ private
153
+ # Use callbacks to share common setup or constraints between actions.
154
+ def set_tag_list
155
+ @tag_list = TagList.find(params[:id])
156
+ end
157
+
158
+ # Never trust parameters from the scary internet, only allow the white list through.
159
+ def tag_list_params
160
+ params.require(:tag_list).permit(:tag_id, :list_id)
161
+ end
162
+ end
20
163
  ```
21
164
 
22
- ### 試したこと
23
165
 
24
- ここに問題に対して試したことを記載してください。
25
166
 
26
167
  ### 補足情報(FW/ツールのバージョンなど)
27
168
 
28
- ここにより詳細な情報を記載してください。
169
+ rails 5.1.7