前提・実現したいこと
いつもお世話になっております。今回もまたよろしくお願いします。
ActiveSrageとS3を使って画像投稿機能を実装しています.
app/views/tours/new.html.haml
で登録したtourモデルと紐づいた画像をapp/views/tours/show.html.haml
に表示したいのですが、- if @tour.image.attached?
節でfalseが出てしまい画像の表示ができません。
S3のバケットから画像を確認することができるので、S3側の問題ではないと思うのですが、わかりません。。。
コンソールでいくつか実験したものも載せておくので一度目を通していただければと思います
お知恵をお貸しくださいませ
また、Tourモデルのtourimage1,tourimage2,tourimage3カラムは今回の件と関連性はないので、無視していただいて大丈夫です。
コンソールでの実験
Tour.firstが新規登録で作成したツアーです
[1] pry(main)> tour=Tour.first Tour Load (0.2ms) SELECT "tours".* FROM "tours" ORDER BY "tours"."id" ASC LIMIT ? [["LIMIT", 1]] => #<Tour:0x000000000540e7f0 id: 1, tourname: "aaaaa", tourcontent: "aaaaaa", user_id: 1, created_at: Wed, 14 Aug 2019 04:34:47 UTC +00:00, updated_at: Wed, 14 Aug 2019 04:34:47 UTC +00:00, tour_image1: nil, tour_image2: nil, tour_image3: nil> [2] pry(main)> tour.image => #<ActiveStorage::Attached::One:0x00000000055a57a8 @dependent=:purge_later, @name="image", @record= #<Tour:0x000000000540e7f0 id: 1, tourname: "aaaaa", tourcontent: "aaaaaa", user_id: 1, created_at: Wed, 14 Aug 2019 04:34:47 UTC +00:00, [3] pry(main)> tour.image.attached? ActiveStorage::Attachment Load (0.2ms) 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", "Tour"], ["name", "image"], ["LIMIT", 1]] => false
該当のソースコード
/config/strage.yml
test: service: Disk root: <%= Rails.root.join("tmp/storage") %> local: service: Disk root: <%= Rails.root.join("storage") %> # Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) amazon: service: S3 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> region: ap-northeast-1 bucket: new-app2
Rails.application.configure do config.cache_classes = false config.active_storage.service = :amazon end
app/controller/tours_controller.rb
class ToursController < ApplicationController before_action:check_user_login?, only:[:show,:new,:create,:edit,:update,:destroy] def show tour_find_by_id @like=Like.new @likes = Like.where(tour_id: @tour.id) @comment = Comment.new @comments = Comment.where(tour_id: @tour.id) end private def tour_params params.require(:tour).permit(:tourname,:tourcontent, :tag_list, :tour_image1,:tour_image2,:tour_image3,:image) end def tour_find_by_id @tour=Tour.find_by(id:params[:id]) end end
app/model/tour.rb
class Tour < ApplicationRecord belongs_to :user has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user has_many :comments, dependent: :destroy has_many :favorites, foreign_key: 'tour_id', dependent: :destroy has_many :users, through: :favorites validates :user_id,presence:true has_one_attached :image
app/views/tours/new.html.haml
%h2.page_title ツアー作成ページ = simple_form_for(@tour) do |f| = f.error_notification .form-inputs.form_group = f.input :tourname, input_html: { autocomplete: "tourname",class:"form-control" } = f.input :tourcontent, input_html: { autocomplete: "tourcontent",class:"form-control"} = f.input :image, as: :file, input_html: { class:"mb-3" } = f.input :tour_image1, as: :file, input_html: { class:"mb-3" } = f.input :tour_image2, as: :file, input_html: { class:"mb-3" } = f.input :tour_image3, as: :file, input_html: { class:"mb-3" } = f.button :submit, "ツアーを作る", input_html: { class:" btn btn-primary " }
app/views/tours/show.html.haml
%smallタイトル: #{@tour.tourname} %h6.card-subtitle.mb-2.text-muted %small製作者: =link_to(@tour.user.username,user_path(@tour.user.id)) %p.card-text - if @tour.image.attached? = image_tag @tour.image
あなたの回答
tips
プレビュー