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には何も表示されません。
よろしくお願いします。
あなたの回答
tips
プレビュー