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

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

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

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

Q&A

解決済

1回答

792閲覧

投稿した画像やテキストをindex.html.erbで表示させたい

tt0913

総合スコア6

Ruby on Rails 6

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

0グッド

0クリップ

投稿2022/03/16 10:31

編集2022/03/16 12:46

現在WEBアプリケーションを作成しており、投稿機能を実装しております。
しかし投稿した画像やテキストがviewに表示されません。
なお画像投稿にはActive Storageを導入しております。
投稿した内容もシークエルプロで確認したところ保存されております。

実際のエラー文は以下です。イメージ説明
関係するファイルを添付させて頂きます。

views/tweets/index.html.erb

1<link href="https://fonts.googleapis.com/css?family=DM+Sans:400,500,700&display=swap" rel="stylesheet"> 2<div class="app-wrapper"> 3 <div class="left-area hide-on-mobile"> 4 <div class="app-header">Aller<br> 5 <span class="inner-text">a</span> 6 <button class="close-menu"> 7 <svg width="24" height="24" fill="none" stroke="#51a380" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="feather feather-x"> 8 <defs /> 9 <path d="M18 6L6 18M6 6l12 12" /> 10 </svg> 11 </button> 12 </div> 13 <div class="left-area-content"> 14 <div class="profile"> 15 <%= image_tag '/assets/sample.jpeg' %> 16 <div class="profile-info"> 17 <span class="profile-name">名前さん</span> 18 </div> 19 </div> 20 <div class="page-link-list"> 21 <a href="#" class="item-link" id="pageLink"> 22 <svg class="link-icon" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="feather feather-home" viewBox="0 0 24 24"> 23 <defs /> 24 <path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z" /> 25 <path d="M9 22V12h6v10" /></svg> 26 マイページ</a> 27 <a href="#" class="item-link" id="pageLink"> 28 <svg class="link-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-heart"> 29 <path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z" /> 30 </svg> 31 記録</a> 32 <a href="#" class="item-link" id="pageLink"> 33 <svg class="link-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-play"> 34 <path d="M5 3l14 9-14 9V3z" /> 35 </svg> 36 おすすめメニュー</a> 37 <a href="#" class="item-link" id="pageLink"> 38 <svg class="link-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-list"> 39 <path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01" /> 40 </svg> 41 寄付</a> 42 <a href="#" class="item-link" id="pageLink"> 43 <svg class="link-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clock"> 44 <circle cx="12" cy="12" r="10" /> 45 <polyline points="12 6 12 12 16 14" /></svg> 46 History</a> 47 </div> 48 49 50 </div> 51 <button class="btn-invite">Invite Team</button> 52 </div> 53 <div class="right-area"> 54 <div class="right-area-upper"> 55 <button class="menu-button"> 56 <svg width="24" height="24" fill="none" stroke="#51a380" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"> 57 <defs /> 58 <path d="M3 12h18M3 6h18M3 18h18" /> 59 </svg> 60 </button> 61 <div class="search-part-wrapper"> 62 <input class="search-input" type="text" placeholder="Search tweets..."> 63 <a class="menu-links" href="#">検索</a> 64 </div> 65 66 <div class="action-buttons-wrapper"> 67 <% if user_signed_in? %> 68 <button class="action-buttons btn-record"><%= link_to current_user.nickname, root_path, class: "user-nickname" %></button> 69 <button class="action-buttons btn-upload"><%= link_to 'ログアウト', destroy_user_session_path , method: :delete, class: "logout" %></button> 70 <% else %> 71 <button class="action-buttons btn-record"><%= link_to 'ログイン', new_user_session_path, class: "login" %></button> 72 <button class="action-buttons btn-upload"><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %></button> 73 <% end %> 74 </div> 75 </div> 76 <div class="page-right-content"> 77 <div class="content-line content-line-hero"> 78 <div class="line-header"> 79 <span class="header-text">トップページ</span> 80 </div> 81 <div class="slider-wrapper owl-carousel owl-theme" id="owl-slider-1"> 82 <div class="item hero-img-wrapper img-1"> 83 <div class="upload-text-wrapper"> 84 <p class="upload-text-info">Aller <span> a</span> 85 <button class="btn-new-1"><%= link_to "つぶやき", new_tweet_path %></button> 86 <button class="btn-new-2">寄付募集</button> 87 </p> 88 </div> 89 <%= image_tag '/assets/food',:size => '500x300'%> 90 </div> 91 </div> 92 </div> 93 <div class="content-line content-line-list"> 94 <div class="line-header"> 95 <span class="header-text">つぶやき</span> 96 </div> 97 <div id="owl-slider-2" class="slider-wrapper owl-carousel"> 98 <div class="item video-box-wrapper"> 99 <div class="img-preview"> 100 <%= image_tag @tweets.image %> 101 </div> 102 <div class="video-description-wrapper"> 103 <p class="video-description-header"><%= @tweets.title %></p> 104 <p class="video-description-subheader"><%#= "by#{tweet.user.name}" %></p> 105 <p class="video-description-info">いいね <span><%#= tweet.created_at %></span></p> 106 </div> 107 </div> 108 <div class="item video-box-wrapper"> 109 <div class="img-preview"> 110 <img src="https://images.unsplash.com/photo-1531736275454-adc48d079ce9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80" alt="video"> 111 </div> 112 <div class="video-description-wrapper"> 113 <p class="video-description-header">Puppet Theatre</p> 114 <p class="video-description-subheader">By July</p> 115 <p class="video-description-info">116K views <span>1 hour ago</span></p> 116 </div> 117 </div> 118 </div> 119 </div> 120 <div class="content-line content-line-list"> 121 <div class="line-header"> 122 <span class="header-text">寄付</span> 123 </div> 124 <div id="owl-slider-3" class="slider-wrapper owl-carousel"> 125 <div class="item video-box-wrapper"> 126 <div class="img-preview"> 127 <img src="https://images.unsplash.com/photo-1494252713559-f26b4bf0b174?ixlib=rb-1.2.1&auto=format&fit=crop&w=2250&q=80" alt="video"> 128 </div> 129 <div class="video-description-wrapper"> 130 <p class="video-description-header">Minimal Photography</p> 131 <p class="video-description-subheader">By July</p> 132 <p class="video-description-info">116K views <span>1 hour ago</span></p> 133 </div> 134 </div> 135 <div class="item video-box-wrapper"> 136 <div class="img-preview"> 137 <img src="https://images.unsplash.com/photo-1524678714210-9917a6c619c2?ixlib=rb-1.2.1&auto=format&fit=crop&w=2249&q=80" alt="video"> 138 </div> 139 <div class="video-description-wrapper"> 140 <p class="video-description-header">Puppet Theatre</p> 141 <p class="video-description-subheader">By July</p> 142 <p class="video-description-info">116K views <span>1 hour ago</span></p> 143 </div> 144 </div> 145 146 </div> 147 </div> 148 </div> 149 </div> 150</div>

models/tweet.rb

1class Tweet < ApplicationRecord 2 belongs_to :user 3 has_one_attached :image 4 5 validates :title, presence: true 6 validates :text, presence: true 7 validates :image, presence: true 8end

controllers/tweets_controller.rb

1class TweetsController < ApplicationController 2 before_action :authenticate_user!, except: [:index] 3 4 def index 5 @tweets = Tweet.all 6 end 7 8 def new 9 @tweet = Tweet.new 10 end 11 12 def create 13 @tweet = Tweet.new(tweet_params) 14 if @tweet.save 15 redirect_to root_path(@tweet) 16 else 17 render :new 18 end 19 end 20 21 22 private 23 24 def tweet_params 25 params.require(:tweet).permit(:image, :title, :text).merge(user_id: current_user.id) 26 end 27end

config.routes.rb

1Rails.application.routes.draw do 2 devise_for :users 3 root to: "tweets#index" 4 resources :tweets 5end

migrate

1class CreateTweets < ActiveRecord::Migration[6.0] 2 def change 3 create_table :tweets do |t| 4 t.string :title, null: false 5 t.text :text, null: false 6 t.references :user, null: false, foreign_key: true 7 t.timestamps 8 end 9 end 10end

エラー文の内容はimageが空?ということだと推測しますが
コントローラでimageの保存許可も行なっているのでどこが間違っているのか分からない状況です。
※@tweets.imageにしたり色々な記述を試しましたがうまくいきませんでした。

rails初心者で大変申し訳ございませんが、アドバイスやご指摘いただけますと幸いです。

どうぞよろしくお願いいたします。

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

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

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

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

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

winterboum

2022/03/16 11:43

indexのviewはそれで全部?
tt0913

2022/03/16 12:47

コメントありがとうございます。 indexのviewを全て記載させて頂きました。 ご確認お願いします。
guest

回答1

0

ベストアンサー

@tweets は Tweet のインスタンスではないです、その配列のようなものです。
ですから Tweetのmethodや属性は持ちません。

それ以前にこのviewはindex内容ではないですね。 @tweetsをeachで回して、各々のtweetの中身を表示するようにしましょう。

投稿2022/03/16 13:10

winterboum

総合スコア23347

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

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

tt0913

2022/03/16 13:31

winterboumさん 丁寧なアドバイスありがとうございます。 @tweetsをeachで回すことで表示することができました。 感謝いたします。 今一度この部分を復習し理解を深めるようにいたします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問