質問編集履歴

2

画像をコードに変更

2021/03/17 10:54

投稿

naonao-program
naonao-program

スコア4

test CHANGED
File without changes
test CHANGED
@@ -32,16 +32,260 @@
32
32
 
33
33
  # 該当するコントローラーの写真
34
34
 
35
+ ```ruby
36
+
37
+ class GymInformationController < ApplicationController
38
+
39
+ before_action :authenticate_user!, only: %i[new create]
40
+
41
+ def index
42
+
43
+ @gyms = GymInformation.includes(:user).order('created_at DESC')
44
+
45
+ end
46
+
47
+
48
+
49
+ def new
50
+
51
+ @gym = GymInformation.new
52
+
53
+ end
54
+
55
+
56
+
57
+ def create
58
+
35
- ![イメージ説明](ed8ce134aacdc3cf927f0f1c1bad9ff4.png)
59
+ @gym = GymInformation.new(gym_information_params)
60
+
61
+ if @gym.save
62
+
63
+ redirect_to root_path
64
+
65
+ else
66
+
67
+ render :new
68
+
69
+ end
70
+
71
+ end
72
+
73
+
74
+
75
+ def show
76
+
77
+ @gym = GymInformation.includes(:user)
78
+
79
+ end
80
+
81
+
82
+
83
+ private
84
+
85
+
86
+
87
+ def gym_information_params
88
+
89
+ params.require(:gym_information).permit(:boulder_or_lead_id, :name, :region_id, :address, :grade_sence_id,
90
+
91
+ :people_day_id, :people_time1_id, :people_time2_id, :people_vibe_id, :clerk_vibe_id, :other, images: []).merge(user_id: current_user.id)
92
+
93
+ end
94
+
95
+ end
96
+
97
+
98
+
99
+ ```
36
100
 
37
101
 
38
102
 
39
103
  # 該当するモデルの写真
40
104
 
105
+ ```ruby
106
+
107
+ class GymInformation < ApplicationRecord
108
+
109
+ belongs_to :user
110
+
111
+ has_many_attached :images
112
+
113
+ extend ActiveHash::Associations::ActiveRecordExtensions
114
+
115
+
116
+
117
+ with_options presence: true do
118
+
119
+ validates :images
120
+
121
+ validates :name
122
+
123
+ validates :address
124
+
41
- ![イメージ説明](6ee8d02e0f8470e6a2c279005c8ad967.png)
125
+ with_options numericality: { other_than: 1 } do
126
+
127
+ validates :boulder_or_lead_id
128
+
129
+ validates :region_id
130
+
131
+ validates :grade_sence_id
132
+
133
+ validates :people_day_id
134
+
135
+ validates :people_day_id
136
+
137
+ validates :people_time1_id
138
+
139
+ validates :people_time2_id
140
+
141
+ validates :people_vibe_id
142
+
143
+ validates :clerk_vibe_id
144
+
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+ extend ActiveHash::Associations::ActiveRecordExtensions
152
+
153
+ belongs_to_active_hash :boulder_or_lead_id
154
+
155
+ belongs_to_active_hash :region_id
156
+
157
+ belongs_to_active_hash :grade_sence_id
158
+
159
+ belongs_to_active_hash :people_day_id
160
+
161
+ belongs_to_active_hash :people_time1_id
162
+
163
+ belongs_to_active_hash :people_time2_id
164
+
165
+ belongs_to_active_hash :people_vibe_id
166
+
167
+ belongs_to_active_hash :clerk_vibe_id
168
+
169
+ end
170
+
171
+ ```
42
172
 
43
173
 
44
174
 
45
175
  #Active Hashのモデルの写真
46
176
 
177
+ ```ruby
178
+
179
+ class Region < ActiveHash::Base
180
+
181
+ self.data = [
182
+
183
+ { id: 1, name: '--' },
184
+
185
+ { id: 2, name: '北海道' },
186
+
187
+ { id: 3, name: '青森県' },
188
+
189
+ { id: 4, name: '岩手県' },
190
+
191
+ { id: 5, name: '宮城県' },
192
+
193
+ { id: 6, name: '秋田県' },
194
+
195
+ { id: 7, name: '山形県' },
196
+
197
+ { id: 8, name: '福島県' },
198
+
199
+ { id: 9, name: '茨城県' },
200
+
47
- ![イメージ説明](cadf351a015a390f9b17a92d3f57c0d4.png)
201
+ { id: 10, name: '栃木県' },
202
+
203
+ { id: 11, name: '群馬県' },
204
+
205
+ { id: 12, name: '埼玉県' },
206
+
207
+ { id: 13, name: '千葉県' },
208
+
209
+ { id: 14, name: '東京都' },
210
+
211
+ { id: 15, name: '神奈川県' },
212
+
213
+ { id: 16, name: '新潟県' },
214
+
215
+ { id: 17, name: '富山県' },
216
+
217
+ { id: 18, name: '石川県' },
218
+
219
+ { id: 19, name: '福井県' },
220
+
221
+ { id: 20, name: '山梨県' },
222
+
223
+ { id: 21, name: '長野県' },
224
+
225
+ { id: 22, name: '岐阜県' },
226
+
227
+ { id: 23, name: '静岡県' },
228
+
229
+ { id: 24, name: '愛知県' },
230
+
231
+ { id: 25, name: '三重県' },
232
+
233
+ { id: 26, name: '滋賀県' },
234
+
235
+ { id: 27, name: '京都府' },
236
+
237
+ { id: 28, name: '大阪府' },
238
+
239
+ { id: 29, name: '兵庫県' },
240
+
241
+ { id: 30, name: '奈良県' },
242
+
243
+ { id: 31, name: '和歌山県' },
244
+
245
+ { id: 32, name: '鳥取県' },
246
+
247
+ { id: 33, name: '島根県' },
248
+
249
+ { id: 34, name: '岡山県' },
250
+
251
+ { id: 35, name: '広島県' },
252
+
253
+ { id: 36, name: '山口県' },
254
+
255
+ { id: 37, name: '徳島県' },
256
+
257
+ { id: 38, name: '香川県' },
258
+
259
+ { id: 39, name: '愛媛県' },
260
+
261
+ { id: 40, name: '高知県' },
262
+
263
+ { id: 41, name: '福岡県' },
264
+
265
+ { id: 42, name: '佐賀県' },
266
+
267
+ { id: 43, name: '長崎県' },
268
+
269
+ { id: 44, name: '熊本県' },
270
+
271
+ { id: 45, name: '大分県' },
272
+
273
+ { id: 46, name: '宮崎県' },
274
+
275
+ { id: 47, name: '鹿児島県' },
276
+
277
+ { id: 48, name: '沖縄県' }
278
+
279
+ ]
280
+
281
+
282
+
283
+ include ActiveHash::Associations
284
+
285
+ has_many :users
286
+
287
+ has_many :gym_information
288
+
289
+ end
290
+
291
+ ```

1

コントローラー モデルの写真追加

2021/03/17 10:54

投稿

naonao-program
naonao-program

スコア4

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,21 @@
27
27
  ```
28
28
 
29
29
  この場合はデータベースに保存されている番号がそのまま出力される
30
+
31
+
32
+
33
+ # 該当するコントローラーの写真
34
+
35
+ ![イメージ説明](ed8ce134aacdc3cf927f0f1c1bad9ff4.png)
36
+
37
+
38
+
39
+ # 該当するモデルの写真
40
+
41
+ ![イメージ説明](6ee8d02e0f8470e6a2c279005c8ad967.png)
42
+
43
+
44
+
45
+ #Active Hashのモデルの写真
46
+
47
+ ![イメージ説明](cadf351a015a390f9b17a92d3f57c0d4.png)