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

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

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

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

Q&A

解決済

1回答

650閲覧

Active Storageで画像が表示できません。

rui-watanabe

総合スコア18

Ruby on Rails

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

0グッド

1クリップ

投稿2019/07/24 10:25

前提・実現したいこと

Rails初心者です。
現在ユーザー画像を表示させたく、Active Storageを使って画像のアップロード機能をつけたのですが、
tweets#indexにuser.avatarで表示させたい時に、NameErrorが起きてしまいます。
記述の間違いと思って、current_user.avatarで表示させたらうまくいいきました。
しかしそれぞれの投稿者で別の表示をさせたいため、その方法を知りたいと思いご質問させていただきました。

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

NameError in Tweets#index Showing /Users/watar/projects/company-love/app/views/tweets/_tweet.html.erb where line undefined local variable or method `user' for #<#<Class:0x00007fa972b46900>:0x00007fa978940c18>: <%= image_tag user.avatar, class:"card-img-top",alt:"Card image cap" %></div>

該当のソースコード(tweets/index.html.erb)

index.html.erb

1<div class="contents row"> 2 <% @tweets.each do |tweet| %> 3 <%= render partial: "tweet", locals: { tweet: tweet } %> 4 <% end %> 5 <%= paginate(@tweets) %> 6</div> 

_tweet.html.erb

1 2<div class="card-group"> 3 <div class="card" style="width:18rem;"> 4 <%= image_tag user.avatar, class:"card-img-top",alt:"Card image cap" %></div> 5 <div class="card-body"> 6 <h4 class="card-title"><%= tweet.user.name %></h4> 7 <p class="card-text"><%= tweet.text%></p> 8 <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> 9 <div class="row"> 10 <div class="col-md"> 11 <button class="btn btn-info"> 12 <%= link_to '詳細', "/tweets/#{tweet.id}", method: :get%> 13 </button> 14 <% if user_signed_in? && current_user.id == tweet.user_id %> 15 <button class="btn btn-info"> 16 <%= link_to '編集', "/tweets/#{tweet.id}/edit", method: :get %> 17 </button> 18 <button class="btn btn-info"> 19 <%= link_to '削除', "/tweets/#{tweet.id}"%> 20 </button> 21 <% end %> 22 </ul> 23 </div> 24 </div> 25 </div> 26 </div> 27</div>

関連するソースコード(application_controller.rb)

application_controller.rb

1class ApplicationController < ActionController::Base 2 3 protect_from_forgery with: :exception 4 before_action :configure_permitted_parameters, if: :devise_controller? 5 add_flash_types :success, :info, :warning, :danger 6 7 def after_sign_out_path_for(resource) 8 home_path # サインアウト後のリダイレクト先 9 end 10 11 def configure_permitted_parameters 12 devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :avatar]) 13 end 14end

関連するソースコード(tweets_controller.rb)

tweets_controller.rb

1class TweetsController < ApplicationController 2 3 before_action :move_to_index, except: [:index, :show] 4 5 def index 6 @tweets = Tweet.includes(:user).page(params[:page]).per(5).order("created_at DESC") 7 end 8 9 def new 10 end 11 12 def create 13 Tweet.create(image: tweet_params[:image], text: tweet_params[:text], user_id: current_user.id) 14 redirect_to root_path 15 end 16 17 def destroy 18 tweet = Tweet.find(params[:id]) 19 tweet.destroy if tweet.user_id == current_user.id 20 redirect_to root_path 21 end 22 23 def edit 24 @tweet = Tweet.find(params[:id]) 25 end 26 27 def update 28 tweet = Tweet.find(params[:id]) 29 if tweet.user_id == current_user.id 30 tweet.update(tweet_params) 31 redirect_to root_path 32 end 33 end 34 35 def show 36 @tweet = Tweet.find(params[:id]) 37 @comments = @tweet.comments.includes(:user) 38 end 39 40 private 41 def tweet_params 42 params.permit(:image, :text) 43 end 44 45 def move_to_index 46 redirect_to action: :index unless user_signed_in? 47 end 48end 49 50 51

前提・実現したいこと

gemは、'mini_magick'が入っており、rails active_storage:install
rails db:migrateしてテーブルを作成し、サインアップ時の画像のレコードも確認できる。

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

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

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

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

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

guest

回答1

0

自己解決

基本的なことで、変数の呼び出しただ単に間違っているだけでした。

eachでtweetを呼び出しているので、tweet.user.avatarとすれば解決しました。

おさわがせしました。

投稿2019/07/25 03:21

rui-watanabe

総合スコア18

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問