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

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

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

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

Q&A

解決済

1回答

4689閲覧

ユーザーが投稿した複数画像を一枚ずつ表示したい

KayoFujii

総合スコア17

Ruby on Rails

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

0グッド

0クリップ

投稿2019/05/27 09:51

編集2019/05/27 10:17

前提・実現したいこと

ツイートにaccepts_nested_attributes_forメソッドで定義して加えた
複数画像を一枚ずつ表示したい。

tweetモデルにaccepts_nested_attributes_forメソッドでtweet_imagesモデルのimageカラムを追加し、複数画像を投稿できるようにしました。その画像を一枚ずつ表示したいのですが、どう書けばいいかわかりません。
配列で一つずつ指定しようかと試みましたが、エラーがでます。
画像アップロードのgemはcarrierwaveを使っています。

発生している問題・エラーメッセージ

NoMethodError in Tweets#show Showing C:/Users/kayo/Desktop/ns-fruit/app/views/tweets/show.html.erb where line #13 raised: undefined method `image' for #<TweetImage::ActiveRecord_Associations_CollectionProxy:0x00000010bbfd18> Extracted source (around line #13): <div class="carousel-inner"> <div class="carousel-item.active"> <%= image_tag @tweet_images.image[0].urls%> </div> <div class="carousel-item"> <%= image_tag @tweet_images.image[1].to_s %>

該当のソースコード

tweets/show/html.erb

<div class="container"> <div class="row"> <div class="col-md-1"> </div> /* Bootstrapのカルーセル */ <div id="carouselControls" class="carousel.slide" data-ride="carousel" style="width:400px"> /* スライドする画像の表示 */ <div class="carousel-inner"> <div class="carousel-item.active"> <%= image_tag @tweet_images.image[0].urls%> </div> <div class="carousel-item"> <%= image_tag @tweet_images.image[1].to_s %> </div> <div class="carousel-item"> <%= image_tag @tweet_images.image[2].to_s %> </div> </div> </div> <div class="col-md-10 text-center"> <strong><%= @tweet.title %></strong> <div class="my-4"><%= image_tag @tweet.image.to_s,width:300,height:300, class:"tweet_image rounded" %> <% @tweet_images.each do |t| %> <%= image_tag t.image.to_s,width:300,height:300,class: "tweet_image rounded" %> <% end %> <% if current_user.already_liked?(@tweet) %> <%= button_to 'いいねを取り消す', tweet_like_path(@tweet), method: :delete, class: "btn-gradient-radius"%> <% else %> <%= button_to 'いいね', tweet_likes_path(@tweet), class: "btn-gradient-radius" %> <% end %> </div> <table class="table table-bordered"> <thead> <tr> <th>ユーザー名</th> <td> <%= link_to @tweet.user.name, user_path(@tweet.user.id) %></td> </tr> <tr> <th>生産地</th> <td><%= @tweet.area %></td> </tr> <tr> <th>果物の説明</th> <td><%= @tweet.body %></td> </tr> <tr> <th>購入方法</th> <td><%= @tweet.means %></td> </tr> <tr> <th>URL</th> <td><%= raw Rinku.auto_link(h(@tweet.url)) %></td> </tr> <tr> <th>いいね件数</th> <td><%= @tweet.likes.count %></td> </tr> <tr> <th>投稿日</th> <td><%= @tweet.created_at %></td> </tr> </thead> </table> <div>↓購入のやりとりをする場合はこちらからメールを送ってください↓</div> <div><%= raw Rinku.auto_link(h(@tweet.user.email)) %></div> <div class="card my-4"> <strong class="card-title">コメント一覧</strong> <div class="card-body"> <% @comments.each do |c| %> <a href="/users/<%= @tweet.user.id %>"><%= c.user.name %></a> <%= c.body %> <br> <% if c.user_id == current_user.id %> <%= link_to "編集する", tweet_comment_path(@tweet.id,c.id),method: :patch %> <%= link_to "削除する", tweet_comment_path(c.id), method: :delete %> <% end %> <% end %> <%= form_for [@tweet, @comment] do |f| %> <%= f.text_field :body %> <%= f.submit 'コメントする', class: "btn-gradient-radius" %> <% end %> </div> </div> <div class="card my-4"> <strong class="card-title">レビュー一覧</strong> <div class="card-body"> <% @reviews.each do |r| %> <div><%= image_tag r.image.to_s, width:100 %></div> <a href="/users/<%= @tweet.user.id %>"><%= r.user.name %></a> <br> <%= r.content %> <br> <% if r.user_id == current_user.id %> <%= link_to "削除する", tweet_review_path(r.id), method: :delete %> <% end %> <% end %> <%= form_for [@tweet, @review] do |f| %> <%= f.label :画像を選択 %> <%= f.file_field :image %> <br> <%= f.text_field :content %> <%= f.submit 'レビューする', class: "btn-gradient-radius" %> <% end %> </div> </div> <!-- <div>いいね件数: </div> --> <!-- <div>いいねしたユーザー</div> <% @tweet.liked_users.each do |user| %> <li><%= user.name %></li> <% end %> --> <%= link_to "ツイート一覧に戻る", tweets_path %> </div> <div class="col-md-1"> </div> </div> </body>

tweets_controller.rb

class TweetsController < ApplicationController #加える行始まり #ログインしていなかったら、このコントローラーの全ての機能を使えない before_action :authenticate_user! #加える行終わり #全ての投稿を表示する画面 def index # Orderで並び替え @tweets = Tweet.all.order("id DESC") if request.post? if params[:search] == nil @tweets = Tweet.all elsif params[:search] == "" @tweets = Tweet.all else #部分検索 @tweets = Tweet.where("title LIKE ? ",'%' + params[:search] + '%') end end end #新しいTweetを投稿する時に使う def new @tweet = Tweet.new 3.times{@tweet.tweet_images.build} end #新しいTweetの保存 def create @tweet = Tweet.new(tweet_params) #現在ログインしているUserのidをTweetのuser_idという部分にセット @tweet.user_id = current_user.id puts "create!" #新しいTweetの保存に成功した場合 if @tweet.save #index.html.erbにページが移る redirect_to action: "index" puts "success" #新しいTweetsの保存に失敗した場合 else #もう一回投稿画面へ puts @tweet.errors.full_messages puts "miss" redirect_to action: "new" end end #投稿の詳細表示 def show @tweet = Tweet.find(params[:id]) @like = Like.new @comments = @tweet.comments @comment = Comment.new @reviews = @tweet.reviews @review = Review.new @tweet_images = @tweet.tweet_images @tweet_image = TweetImage.new end #投稿の編集 def edit @tweet = Tweet.find(params[:id]) end #投稿の削除 def destroy Tweet.find(params[:id]).destroy redirect_to action: :index end #アップデート def update @tweet = Tweet.find(params[:id]) if @tweet.update(tweet_params) redirect_to :action => "show", :id => @tweet.id else redirect_to :action => "new" end end #Viewのformで取得したパラメータをモデルに渡す def search end private #セキュリティのため、許可した:bodyというデータだけ取ってくるようにする def tweet_params params.require(:tweet).permit(:body, :image, :title, :url, :area, :means, :description, tweet_images_attributes: [:image]) end end

試したこと

accepts_nested_attributes_forについて調べた。imageをimagesに替えた

補足情報(FW/ツールのバージョンなど)

参考にした記事
リンク

リンク

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

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

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

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

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

guest

回答1

0

ベストアンサー

ちなみに、TweetImage model は、どのようになっているでしょか。

app/views/tweets/show.html.erb

@tweet_images.image[0]

と書いてありますが、

@tweet_images[0].image

又は、

@tweet_images[0].images.to_s

などで、表示はできないでしょうか。

投稿2019/06/02 16:52

fshun

総合スコア261

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

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

KayoFujii

2019/06/03 06:04

ありがとうございます! @tweet_images[0].images.to_sにしたところできました!
fshun

2019/06/03 07:27 編集

良かったです!! `@tweet_images = @tweet.tweet_images` の部分で 、 ActiveRecord Associations で Tweetに紐づく画像が Arrayに入って返ってくるので、 そのような書き方になります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問