前提・実現したいこと
ツイートに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/ツールのバージョンなど)
参考にした記事
リンク
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/03 06:04
2019/06/03 07:27 編集