質問編集履歴
2
追加でエラーが発生してしまいました
title
CHANGED
File without changes
|
body
CHANGED
@@ -178,4 +178,177 @@
|
|
178
178
|
# end
|
179
179
|
|
180
180
|
end
|
181
|
-
```
|
181
|
+
```
|
182
|
+
|
183
|
+
###追記でエラーが発生しました
|
184
|
+
|
185
|
+
いただいた通り、albumの下に入れ子を作ってみるとかなり複雑っぽくなってしまい、多々エラーが発生してしまいました。。泣
|
186
|
+
まず、直したいところは下記2点です。
|
187
|
+
|
188
|
+
・active admin上で、アルバムとレストランを紐付けたい
|
189
|
+
現在はアルバムを選択する項目が消えてしまいました。。下記はレストランを入力するフォームです。
|
190
|
+

|
191
|
+
|
192
|
+
active_admin側はgemで
|
193
|
+
|
194
|
+
・アルバムの画像を表示させるエラーが出てしまいます
|
195
|
+
NoMethodErrorとなってしまい、ビューの書き方に問題があるかと考えられます。
|
196
|
+
|
197
|
+
・active admin上でアルバムを入力するビューになぜか、アルバムIDが不明に2つ出ています。
|
198
|
+

|
199
|
+
|
200
|
+
|
201
|
+
ビュー
|
202
|
+
```Ruby
|
203
|
+
<!-- Wrapper for slides -->
|
204
|
+
<div class="carousel-inner" role="listbox">
|
205
|
+
<div class="item active">
|
206
|
+
<%= image_tag @restaurant.image %>
|
207
|
+
</div>
|
208
|
+
<div class="item restaurant-image">
|
209
|
+
<%= image_tag @restaurant.album.image_1 %>
|
210
|
+
</div>
|
211
|
+
<div class="item restaurant-image">
|
212
|
+
<%= image_tag @restaurant.album.image_2 %>
|
213
|
+
</div>
|
214
|
+
<div class="item restaurant-image">
|
215
|
+
<%= image_tag @restaurant.album.image_3 %>
|
216
|
+
</div>
|
217
|
+
<div class="item restaurant-image">
|
218
|
+
<%= image_tag @restaurant.album.image_4 %>
|
219
|
+
</div>
|
220
|
+
<div class="item restaurant-image">
|
221
|
+
<%= image_tag @restaurant.album.image_5 %>
|
222
|
+
</div>
|
223
|
+
<div class="item restaurant-image">
|
224
|
+
<%= image_tag @restaurant.album.image_6 %>
|
225
|
+
</div>
|
226
|
+
</div>
|
227
|
+
```
|
228
|
+
|
229
|
+
**■ レストラン側のコード**
|
230
|
+
|
231
|
+
・レストランのモデル
|
232
|
+
```Ruby
|
233
|
+
class Restaurant < ApplicationRecord
|
234
|
+
belongs_to :area
|
235
|
+
has_many :album
|
236
|
+
mount_uploader :image, ImageUploader
|
237
|
+
geocoded_by :mapaddress
|
238
|
+
after_validation :geocode
|
239
|
+
end
|
240
|
+
```
|
241
|
+
・レストランのコントローラー(restaurant_controllers.rb)
|
242
|
+
```Ruby
|
243
|
+
class RestaurantsController < ApplicationController
|
244
|
+
・・・
|
245
|
+
def restaurant_params
|
246
|
+
params.require(:restaurant).permit(:id, :name, :image, :genre, :access, :hour,
|
247
|
+
:address, :phone, :website,
|
248
|
+
:description, :seats, :area_id, :album_id, :mapaddress, :latitude, :longitude, albums_attributes: [:image])
|
249
|
+
end
|
250
|
+
・・・
|
251
|
+
end
|
252
|
+
```
|
253
|
+
・レストランとアルバムを紐付けるマイグレーションファイル
|
254
|
+
日付_album_id_to_restaurants.rb
|
255
|
+
```Ruby
|
256
|
+
class AlbumIdToRestaurants < ActiveRecord::Migration[5.0]
|
257
|
+
def self.up
|
258
|
+
add_column :restaurants, :album_id, :integer
|
259
|
+
add_index :restaurants, :album_id
|
260
|
+
end
|
261
|
+
|
262
|
+
def self.down
|
263
|
+
remove_index :restaurants, :column => :album_id
|
264
|
+
remove_column :restaurants, :album_id
|
265
|
+
end
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
**■ アルバム側のコード**
|
270
|
+
・アルバムのモデル
|
271
|
+
```Ruby
|
272
|
+
class Album < ApplicationRecord
|
273
|
+
belongs_to :restaurant
|
274
|
+
has_many :album_contents
|
275
|
+
accepts_nested_attributes_for :album_contents
|
276
|
+
end
|
277
|
+
```
|
278
|
+
|
279
|
+
・アルバムのコントローラー
|
280
|
+
```Ruby
|
281
|
+
class AlbumController < ApplicationController
|
282
|
+
def new
|
283
|
+
@album = Album.new(create_params)
|
284
|
+
9.times { @album.album_contents.build }
|
285
|
+
end
|
286
|
+
|
287
|
+
private
|
288
|
+
|
289
|
+
def create_params
|
290
|
+
params.require(:album).permit(album_contents_attributes: [:image])
|
291
|
+
end
|
292
|
+
end
|
293
|
+
```
|
294
|
+
・アルバムのマイグレーションファイル
|
295
|
+
```Ruby
|
296
|
+
class CreateAlbums < ActiveRecord::Migration[5.0]
|
297
|
+
def change
|
298
|
+
create_table :albums do |t|
|
299
|
+
t.integer :restaurant_id
|
300
|
+
|
301
|
+
t.timestamps
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
```
|
306
|
+
・アルバムの入れ子となるアルバムコンテンツのモデル
|
307
|
+
```Ruby
|
308
|
+
class AlbumContent < ApplicationRecord
|
309
|
+
belongs_to :album
|
310
|
+
mount_uploader :image_1, AlbumContentUploader
|
311
|
+
mount_uploader :image_2, AlbumContentUploader
|
312
|
+
mount_uploader :image_3, AlbumContentUploader
|
313
|
+
mount_uploader :image_4, AlbumContentUploader
|
314
|
+
mount_uploader :image_5, AlbumContentUploader
|
315
|
+
mount_uploader :image_6, AlbumContentUploader
|
316
|
+
mount_uploader :image_7, AlbumContentUploader
|
317
|
+
mount_uploader :image_8, AlbumContentUploader
|
318
|
+
mount_uploader :image_9, AlbumContentUploader
|
319
|
+
end
|
320
|
+
```
|
321
|
+
|
322
|
+
・アルバムコンテンツのマイグレーションファイル
|
323
|
+
```Ruby
|
324
|
+
class CreateAlbumContents < ActiveRecord::Migration[5.0]
|
325
|
+
def change
|
326
|
+
create_table :album_contents do |t|
|
327
|
+
t.integer :album_id
|
328
|
+
t.string :image_1
|
329
|
+
t.string :image_2
|
330
|
+
t.string :image_3
|
331
|
+
t.string :image_4
|
332
|
+
t.string :image_5
|
333
|
+
t.string :image_6
|
334
|
+
t.string :image_7
|
335
|
+
t.string :image_8
|
336
|
+
t.string :image_9
|
337
|
+
|
338
|
+
t.timestamps
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
```
|
343
|
+
|
344
|
+
・アルバムとアルバムコンテンツを紐付けるマイグレーションファイル
|
345
|
+
```Ruby
|
346
|
+
class AddAlbumRefToAlbumContent < ActiveRecord::Migration[5.0]
|
347
|
+
def change
|
348
|
+
add_column :album_contents, :album, :refernces
|
349
|
+
end
|
350
|
+
end
|
351
|
+
```
|
352
|
+
|
353
|
+
いろいろ反省しかありません。。
|
354
|
+
すみませんが、おたすけくださいませ。
|
1
Railsのバージョンを間違えました
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
carrierwaveを使って、レストランページにて複数画像をアップロードしたいのですが、**なぜか一つの画像を参照**してしまいます。
|
5
5
|
アップロードは**active admin**から行なっています。
|
6
6
|
|
7
|
-
開発環境:Ruby on Rails 5.0.0.
|
7
|
+
開発環境:Ruby on Rails 5.0.0.1
|
8
8
|
|
9
9
|
① それぞれ画像をアップロードしても...
|
10
10
|

|