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

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

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

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

Q&A

0回答

585閲覧

rails6 activestorageで保存した画像が表示できない

ikd25

総合スコア4

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/10 13:42

activestorageを使って画像を保存してviewに表示したいのですが表示できません。
どこが違うか分かる方がいたらよろしくお願いします。

以下やったことです。

$ rails active_storage:install $ rails db:migrate

ruby

1# models/post.rb 2 3has_one_attached :image

html

1<%= form_with url: posts_path do |f| %> 2 <div class='field'> 3 <%= f.label :title, 'タイトル' %><br> 4 <%= f.text_area :title %><br> 5 </div> 6 <div class='field'> 7 <%= f.label :content, "コンテンツ" %><br> 8 <%= f.rich_text_area :content %> 9 </div> 10 <div class='field'> 11 <%= f.label :image, "サムネイルを設定" %><br> 12 <%= f.file_field :image %> 13 </div> 14 <%= f.submit "送信" %> 15 <% end %>

html

1<!-- posts/index.html.erb --> 2 3<% @posts.each do |post| %> 4 <% if post.image.attached? %> 5 <%= image_tag post.image %><br> 6 <% end %> 7 <%= link_to("title: #{post.title}", "/posts/#{post.id}") %><br> 8<% end %>

ruby

1# config/environments/development.rb 2 3config.active_storage.service = :local
[config/storage.yml] test: service: Disk root: <%= Rails.root.join("tmp/storage") %> local: service: Disk root: <%= Rails.root.join("storage") %>
Started GET "/posts" for ::1 at 2021-04-10 22:36:02 +0900 Processing by PostsController#index as HTML Rendering layout layouts/application.html.erb Rendering posts/index.html.erb within layouts/application User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:2 Post Load (0.7ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."created_at" DESC ↳ app/views/posts/index.html.erb:15 ActiveStorage::Attachment Load (0.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 9], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 8], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 7], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 6], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 5], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 4], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 2], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16 ActiveStorage::Attachment Load (0.4ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 1], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:16

こんな感じになっています.
新規投稿を作成してcreateされてredirectはされるのですがviewには何も表示されません。
よろしくお願いします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問