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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Amazon S3

Amazon S3 (Simple Storage Service)とはアマゾン・ウェブ・サービスが提供するオンラインストレージサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

0回答

1499閲覧

【Rails】Active StrageとS3に保存された画像が表示されません...

k_yusuke

総合スコア19

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Amazon S3

Amazon S3 (Simple Storage Service)とはアマゾン・ウェブ・サービスが提供するオンラインストレージサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2019/08/14 08:12

前提・実現したいこと

いつもお世話になっております。今回もまたよろしくお願いします。
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

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問