質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

215閲覧

画像表示が文字列になってしまう現状を修正し、複数画像の表示を実装したい。

ryongon

総合スコア5

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/03/18 08:28

編集2020/03/18 08:34

フリマアプリ(メルカリのような見た目)の商品詳細ページの実装中です。
1つのテーブルにある複数の画像を表示させたいのですが、上手くいきません。
ご教授お願い致します。(文字が表示されているので、それだけでも直したいです。)

環境

Rails 5.2.4.1
ruby 2.5.1p57

条件

itemsテーブルのidとimagesテーブルのitem_idが一致する画像を複数表示させようと考えています。

ソースコード

ruby

1class ItemsController < ApplicationController 2 3 def new 4 end 5 6 def create 7 Post.create() 8 end 9 10 def show 11 @item = Item.find(params[:id]) 12 @category = Category.find(@item.category_id) 13 @image = Image.where(['item_id LIKE ?', "%#{@item.id}%"] ) 14 end 15 16end

ruby

1# show.html.haml 2 3.header 4 = render 'top/header' 5 6 .border 7 8 .katrgori 9 .katrgori__1 10 FURIMA 11 .katrgori__ku 12 > 13 .katrgori__2 14 ベビー・キッズベビー服(男女兼用) 15 .katrgori__ku 16 > 17 .katrgori__3 1895cmアウター 19 .katrgori__ku 20 > 21 .katrgori__4 22 product3 23 24.item 25 .item__box 26 .item__box__name 27 .item__box__main 28 .item__box__main__image 29 = @image 30 .item__box__main__sub 31 .item__box__main__sub__test1 32 .item__box__main__sub__test2 33 .item__box__main__sub__test3 34 .item__box__price 35 .item__box__price__include 36 = "¥20000" 37 .item__box__price__fee 38 .item__box__price__fee__tax 39 (税込) 40 .item__box__price__fee__postage 41 送料込み 42 .item__box__method 43 .item__box__method__introduction 44 .item__box__information 45 .item__box__information__table 46 %table 47 %tbody 48 %tr 49 %th 出品者 50 %td 51 = @item.seller_id 52 %tr 53 %th カテゴリー 54 %td 55 = @category.name 56 %tr 57 %th ブランド 58 %td 59 = @item.brand_id 60 %tr 61 %th 商品のサイズ 62 %td 63 = @item.size_id 64 %tr 65 %th 商品の状態 66 %td 67 = @item.condition_id 68 %tr 69 %th 配送料の負担 70 %td 71 = @item.postage_payer_id 72 %tr 73 %th 発送元の地域 74 %td 75 = @item.prefecture_code 76 %tr 77 %th 発送日の目安 78 %td 79 = @item.preparation_day_id 80 .item__box__okiniiri 81 %btn.aaa 82 ★ お気に入り 0 83 -# = link_to("★ お気に入り 0") 84 .item__box__spam 85 %btn.bbb 86 ???? 不適切な商品の通報 87 -# = link_to('???? 不適切な商品の通報') 88 89 .item__comment__box 90 .item__comment__box__body 91 %input 92 .item__comment__box__rule 93 相手のことを考え丁寧なコメントを心がけましょう。 94 %br/ 95 不快な言葉遣いなどは利用制限や退会処分となることがあります。 96 .item__comment__box__submit 97 %btn.ccc 98 = link_to('コメントする') 99 100 .item__link 101 .item__link__a 102 < 前の商品 103 .item__link__b 104 後ろの商品 > 105 106 .item__more 107 ベビー・キッズをもっと見る 108 109.banner 110 .banner__inner 111 %h2.banner__inner__title だれでもかんたん、人生を変えるフリマアプリ 112 %p.banner__inner__text 今すぐ無料ダウンロード! 113 .banner__inner__icon 114 -# <img src="/assets/app-store.svg" width="180px" height="53.33px"> 115 = image_tag "", size: "180x53.33", alt: "test" 116 -# <img src="/assets/google-play.svg" width="180px" height="53.33px"> 117 = image_tag "", size: "180x53.33", alt: "test" 118= render 'top/footer.htlm.haml'

ruby

1 2Rails.application.routes.draw do 3 devise_for :users, controllers: { 4 registrations: 'users/registrations' 5 } 6 devise_scope :user do 7 get 'destinations', to: 'users/registrations#new_destination' 8 post 'destinations', to: 'users/registrations#create_destination' 9 end 10 root to: "top#index" 11 resources :users, only: [:show] do 12 resources :items, only: [:new, :create, :show] 13 get "logout" => "users#logout" 14 resources :credit_card, only: [:new, :show, :index, :edit] do 15 collection do 16 post 'pay', to: 'credit_card#pay' 17 post 'delete', to: 'credit_card#delete' 18 post 'buy/:id', to: 'credit_card#buy' 19 end 20 end 21 end 22 23 24 25 resources :tests, only: [:index, :new, :create,] 26 27 28 get "ncard" => "users#ncard" 29end

readme

1# freemarket_sample_70f 2## usersテーブル 3|Column|Type|Options| 4|------|----|-------| 5|nickname|string|null: false| 6|email|string|null: false, unique, true, index: ture| 7|password|string|null: false| 8|password_confirmation|string|null: false| 9|family_name|string|null: false| 10|first_name|string|null: false| 11|family_name_kana|string|null: false| 12|first_name_kana|string|null: false| 13|birth_year|integer|null: false| 14|birth_month|integer|null: false| 15|birth_day|integer|null: false| 16 17 18### Association 19- has_many : seller_items, foreign_key: "seller_id", class_name: "items" 20- has_many : buyer_items, foreign_key: "buyer_id", class_name: "items" 21- has_one : destination, dependent: :destory 22- has_one : credit_card, dependent: :destroy 23 24 25 26## destinationsテーブル 27|Column|Type|Options| 28|------|----|-------| 29|post_code|inteder|null: false| 30|prefecture_code|integer|null: false| 31|city|string|null: false| 32|house_number|string|null: false| 33|building_number|string|| 34|phone_number|string| 35|user_id|integer|null: false, foreign_key: ture| 36### Association 37- belongs_to :user 38- Gem :jp_prefectureを使用して都道府県コードを取得 39 40 41# credit_cardsテーブル 42|Column|Type|Options| 43|------|----|-------| 44|customer_id|string|null: false| 45|card_id|string|null: false| 46|user_id|integer|null: false, foreign_key: true| 47### Association 48- belongs_to :user 49 50## itemsテーブル 51|Column|Type|Options| 52|------|----|-------| 53|name|string|null: false| 54|introduction|text|null: false| 55|price|integer|null: falce| 56|brand_id|inteder|null: false, foreign_key: ture| 57|condition_id|integer|null: false, foreign_key: ture| 58|postage_payer_id|integer|null: false, foreign_key: true| 59<!-- |postage_type_id|integer|null: false, foreign_key: true| --> 60|prefecture_code|integer| null: false| 61|preparation_day_id|integer|null: false, foreign_key: true| 62|category_id|integer|null: false, foreign_key: true| 63|trading_status|enum|null: false| 64|user_id|integer|null: false, foreign_key: true| 65|seller_id|integer|null: false, foreign_key: true| 66|buyer_id|integer|foreign_key: true) 67<!-- |deal_closed_date|timestamp|| --> 68 69 70### Association 71- has_many :images, dependent: :destroy 72- belongs_to :category 73- belongs_to :brand 74- belongs_to :active_hash :condition 75- belongs_to :active_hash :postage_payer 76<!-- - belongs_to :active_hash :postage_type --> 77- belongs_to :active_hash :preparation_day 78- belongs_to :seller, class_name: "User" 79- belongs_to :buyer, class_name: "User" 80- Gem :jp_prefectureを使用して都道府県コードを取得 81 82## imagesテーブル 83|Column|Type|Options| 84|------|----|-------| 85|item_id|integer|null: false, foreign_key: true| 86|url|string|null: false| 87### Association 88- belongs_to :item 89 90## Categoriesテーブル 91|Column|Type|Options| 92|------|----|-------| 93|name|string|null: false| 94|ancestry|string|null: false| 95### Association 96- has_many :items 97 98 99## brandsテーブル 100|Column|Type|Options| 101|------|----|-------| 102|name|string|| 103### Association 104- has_many :items 105 106 107## sizes(active_hash)モデル 108|Column|Type|Options| 109|------|----|-------| 110|size|string|null: false| 111### Association 112- has_many :items 113 114## conditions(active_hash)モデル 115|Column|Type|Options| 116|------|----|-------| 117|condition|string|null: false| 118### Association 119- has_many :items 120 121## postage_payers(active_hash)モデル 122|Column|Type|Options| 123|------|----|-------| 124|postage_payer|string|null: false| 125### Association 126- has_many :items 127 128## preparation_days(active_hash)モデル 129|Column|Type|Options| 130|------|----|-------| 131|preparation_day|string|null: false| 132### Association 133- has_many :items 134 135## Postage_types(active_hash)モデル 136|Column|Type|Options| 137|------|----|-------| 138|postage_type|string|null: false| 139### Association 140- has_many :items 141 142## S3画像アップロード・条件分岐の現状 143⇨https://master.tech-camp.in/curriculums/3967 144 進捗は上記のカリキュラムの「正しくアップロードできるか確認しよう」の前までは一応完了 145⇨https://qiita.com/15grmr/items/71859caea0414f2e245d 146 上記は参考記事 147

現状のビュー(一部)

https://gyazo.com/df2ef5e34231c52114b28704092beebb

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問