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

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

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

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

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Amazon S3

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

Q&A

解決済

1回答

2721閲覧

投稿した画像をimage_tag もしくはurl_forを使って背景に表示させたい。

yutaro_0529

総合スコア12

Ruby on Rails

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

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Amazon S3

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

0グッド

0クリップ

投稿2020/11/05 13:35

編集2020/11/15 03:22

前提・実現したいこと

Ruby on railsで画像が投稿できるようなシステムを作っています。
画像を背景に表示させたい。

発生している問題・エラーメッセージ

active storageに投稿した画像を保存し表示できるように出来ました。
その後、AWSのS3に投稿して画像を保存して表示させようとすると、エラーが発生し、
HTMLの記述を修正しました。

画像データがレコードには正常に送られているのがわかるのですが、
画像が表示されず困っています。
_item.html.erbの画像を表示させるコードに問題があると思います。

該当のソースコード

Ruby

1app/veiws/veiws/_item.html.erb 2 3<code> 4 5➡︎このコードだと画像が表示されない<div class="content_post" style="background-image: url(<%= image_tag item.image %>) ;"> 6➡︎このコードだとエラーが発生<%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> 7 8➡︎このコードだとエラーが発生(NoMethodError in Users#show)<div class="content_post" style="background-image: url(<%= url_for(item.image.url)=%> 9 <div class="more"> 10 <span><%= image_tag 'arrow_top.png'%></span> 11 <ul class="more_list"> 12 <li> 13 <%= link_to '詳細', item_path(item.id), method: :get%> 14 </li> 15 <% if user_signed_in? && current_user.id == item.user_id %> 16 <li> 17 <%= link_to '削除', item_path(item.id), method: :delete %> 18 </li> 19 <%# Prefixの引数 %> 20 <%# アイテムのID(item.id) %> 21 <%# HTTPメソッドは、DELETEというHTTPメソッド %> 22 <%# methodオプションにシンボルで:delete %> 23 <li> 24 <%= link_to '編集',edit_item_path(item.id), method: :get %> 25 <%# get サーバーからブラウザに情報を返す。単にウェブサイトを閲覧する際など、情報を取得するために利用される。 %> 26 <%# Prefixでパスを確認 %> 27 </li> 28 <% end %> 29 </ul> 30 </div> 31 <p><%= item.text %></p> 32 <span class="name"> 33 <a href="/users/<%= item.user.id %>"> 34 <span>投稿者</span><%= item.user.nickname %> 35 </a> 36 </span> 37 </div> 38>  39

➡︎エラー内容

NoMethodError in Users#show Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: undefined method `url' for #<ActiveStorage::Attached::One:0x00007fc948e1ff28> Extracted source (around line #1): <div class="content_post" style="background-image: url(<%= url_for(item.image.url)=%> <%= image_tag item.image.variant(resize: '500x500'), class: 'item-image' if item.image.attached? %> <%= image_tag message.image, class: 'message-image' if message.image.attached? %> Trace of template inclusion: #<ActionView::Template app/views/users/show.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/users/show.html.erb:4 app/views/users/show.html.erb:3 Request Parameters:

https://gyazo.com/17411c4b60fe029ecbe7f89f2b7156e2

試したこと

app/veiws/veiws/_item.html.erb <code> 上記修正後のコード()の中身 <div class="content_post" style="background-image: url(<%= image_tag item.image %>) ;">  画像が表示されず、エラーなし <div class="content_post" style="background-image: url(<%= image_url item.image %>) ;"> ➡︎1パターン目image_urlを使った場合 Sprockets::Rails::Helper::AssetNotFound in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: The asset "" is not present in the asset pipeline. Extracted source (around line #1): <div class="content_post" style="background-image: url(<%= image_url item.image %>) ;"> <%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> <%# <%= image_tag item.image ,class: 'item-image' if item.image.attached? %> <%# <%= image_tag message.image, class: 'message-image' if message.image.attached? %> <%# attached?メソッド レコードにファイルが添付されているかどうか モデル.ファイル名.attached? %> <%# image_tagメソッド img要素を生成するRailsのヘルパーメソッド %> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 ➡︎2パターン目image_pathを使った場合 Sprockets::Rails::Helper::AssetNotFound in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: The asset "" is not present in the asset pipeline. Extracted source (around line #1): <div class="content_post" style="background-image: url(<%= image_path item.image %>) ;"> <%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 ➡︎3パターン目をitem.image.path使った場合 NoMethodError in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: undefined method `path' for #<ActiveStorage::Attached::One:0x00007fee7e66ff70> Extracted source (around line #1): <div class="content_post" style="background-image: url(<%= item.image.path %>) ;"> <%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> <%# <%= image_tag item.image ,class: 'item-image' if item.image.attached? %> <%# <%= image_tag message.image, class: 'message-image' if message.image.attached? %> <%# attached?メソッド レコードにファイルが添付されているかどうか モデル.ファイル名.attached? %> <%# image_tagメソッド img要素を生成するRailsのヘルパーメソッド %> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 ➡︎4パターン目をitem.image.url使った場合 NoMethodError in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: undefined method `url' for #<ActiveStorage::Attached::One:0x00007fee7f6197c8> Extracted source (around line #1): <div class="content_post" style="background-image: url(<%= item.image.url %>) ;"> <%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 ➡︎5パターン目@item.image.urlを使った場合 NoMethodError in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #1 raised: undefined method `image' for nil:NilClass Extracted source (around line #1): <div class="content_post" style="background-image: url(' <%= image_tag(@item.image.url) %>') ;"> <%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:1 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 ➡︎6パターン目<div class="content_post" style="background-image: url(<%= url_for(item.image) ,class: 'item-image' if item.image.attached? %> 上記のコードでエラーが発生しました。 エラースクリーンショット https://gyazo.com/f1fe843012299d2dc4ec19060bf82905 続き https://gyazo.com/e89d008392120fa0a804ff47f8a90bf3

補足

S3はバケットを作成して、バケットポリシーを編集、 Gemfieにgem "aws-sdk-s3", require: falseをbundle installしました。 その後config/enviroments/delopment.rbにconfig.active_storage.service = :amazonにlocalから変更。 次に「パブリックアクセスをすべてブロック」のチェックを外す。 新規のパブリックバケットポリシー,任意のバケットポリシーにチェックを入れました。 config/storage.ymlに追記 amazon: service: S3 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> region: ap-northeast-1 bucket: 自身のバケット名 環境設定 config/enviloment/developments.rb 最後の記述 # default_url_options[:host] = "localhost:3000" host = 'samplehost' Rails.application.routes.default_url_options[:host] = host #おそらく本番環境productionにも定義 ここを記載してlocalでは表示させることが出来ました。 url_forメソッドを使用して

追記11/6 20:42
1)S3にsaveできたというのは、WEBページのAWSのS3のオブジェクトで確認しました。
2)item_params の中身 => <ActionController::Parameters {"text"=>"パンツ", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007fefc2b84c78 @tempfile=#Tempfile:/var/folders/qp/vfswzv852_x8zy3q27gp1gt00000gn/T/RackMultipart20201106-6566-1l80iav.png, @original_filename="スクリーンショット 2020-11-06 19.46.55.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="item[image]"; filename="\xE3\x82\xB9\xE3\x82\xAF\xE3\x83\xAA\xE3\x83\xBC\xE3\x83\xB3\xE3\x82\xB7\xE3\x83\xA7\xE3\x83\x83\xE3\x83\x88 2020-11-06 19.46.55.png"\r\nContent-Type: image/png\r\n">, "user_id"=>1} permitted: true>
3)item modelの定義
params.require(:item).permit(:text, :image ).merge(user_id: current_user.id

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

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

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

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

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

m.ts10806

2020/11/05 22:57 編集

_item.html.erb のコードを提示してください。
winterboum

2020/11/05 23:02

エラー()が発生し、 とありますが、エラーの中身がわからないですメッセージ全文を載せて
yutaro_0529

2020/11/06 00:21 編集

エラーの中身がなくすみません、ご回答ありがとうございます。 こちらが全文になります。 ``` <div class="content_post" style="background-image: url(<%= image_tag item.image %>) ;"> ➡︎修正前<%# <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> <div class="more"> <span><%= image_tag 'arrow_top.png'%></span> <ul class="more_list"> <li> <%= link_to '詳細', item_path(item.id), method: :get%> </li> <% if user_signed_in? && current_user.id == item.user_id %> <li> <%= link_to '削除', item_path(item.id), method: :delete %> </li> <%# Prefixの引数 %> <%# アイテムのID(item.id) %> <%# HTTPメソッドは、DELETEというHTTPメソッド %> <%# methodオプションにシンボルで:delete %> <li> <%= link_to '編集',edit_item_path(item.id), method: :get %> <%# get サーバーからブラウザに情報を返す。単にウェブサイトを閲覧する際など、情報を取得するために利用される。 %> <%# Prefixでパスを確認 %> </li> <% end %> </ul> </div> <p><%= item.text %></p> <span class="name"> <a href="/users/<%= item.user.id %>"> <span>投稿者</span><%= item.user.nickname %> </a> </span> </div> ```
yutaro_0529

2020/11/06 00:19

読みづらいコメントアウトをつけていたので、 削除しました。
winterboum

2020/11/06 00:56

エラーは? それとcodeは<code>を使って質問欄に書いていただくほうがインデントなどついて読みやすいです
m.ts10806

2020/11/06 00:58

質問は編集できますので、原則質問に追記してください。
yutaro_0529

2020/11/06 04:14

お二人ともすみません。 エラー内容追記、<code>を使って質問内容変更しました。
guest

回答1

0

ベストアンサー

<div class="content_post" style="background-image: url(<%= image_tag item.image %>) ;">
ここですが、image_tag でなく image_url とか image_path とかでないのかな。

投稿2020/11/06 01:00

winterboum

総合スコア23284

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

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

yutaro_0529

2020/11/06 04:15

ご回答ありがとうございます! 2つともコードを反映させてみたのですが、 両方エラーが発生しました。 Sprockets::Rails::Helper::AssetNotFound in Items#index
winterboum

2020/11/06 04:25

エラーメッセージは全文載せて
winterboum

2020/11/06 04:27

あ、ごめんそれassetsのではなく全部S3に置いた画像か。 ちとまって
winterboum

2020/11/06 04:29

S3にどういう手段で入れたかにより変わりますが item.image でなく item.image.path とか item.image.url とかでないかな
yutaro_0529

2020/11/06 07:21

度々、ご回答ありがとうございます!! エラーメッセージを全て載せさせて頂きました!! item.image.path と item.image.url も実行してみましたが、 undefined method `url' forが出て、urlが取得出来ていないということがわかりました。
winterboum

2020/11/06 08:57

いや、そのエラーはitem.image.urlではないということです。 S3へは何を使ってsaveしてますか
yutaro_0529

2020/11/06 10:16

何を使ってsave出来てるかわからないのですが、 create メソッドには、このように記述しました。 def create # Item.create(item_params) # binding.pry item =Item.new(item_params) if item.save redirect_to root_path else render "item/new" end # アイテムモデル保存する # praivateでitem_paramsで引用してくるものを指定 end
winterboum

2020/11/06 10:33

1)S3にsaveできた、というのはどのようにして確認したのでしょう? 2)item_params を見せてください 3) model Itemのimage関係の定義を見せてください
yutaro_0529

2020/11/06 11:58

度々教えて頂きありがとうございます! 質問を編集し、上記の内容記載させていただきました。
winterboum

2020/11/06 12:04

2)の item_paramsとは def item_params のcodeです 3) のmodel Itemのimage関係の定義とは app/modesl/item.rb のcodeです
yutaro_0529

2020/11/06 12:53

2)の item_paramsとは def item_params のcodeです ➡︎ private def item_params params.require(:item).permit(:text, :image ).merge(user_id: current_user.id) end 3) のmodel Itemのimage関係の定義とは app/modesl/item.rb のcodeです ➡︎ <code> class ItemsController < ApplicationController before_action :set_item, only: [:edit, :show] # privateで設定したset_itemを代入する # あとでマイページをランダムで表示できるように before_action :move_to_index, except: [:index, :show, :search] # index.html.erbページにリダイレクト # ログインしていなくても、詳細ページに遷移できる仕様にするためにexcept: [:index, :show]としている def index @items = Item.includes(:user).sample(10) # binding.pry # includesメソッドを使用したためallメソッドは省略、N+1問題解決2回のアクセスで済む end # @items = Item.includes(:user)多分間違いコード def new @item = Item.new end def create # Item.create(item_params) # binding.pry item =Item.new(item_params) # binding.pry if item.save redirect_to root_path else render "item/new" end # アイテムモデル保存する # praivateでitem_paramsで引用してくるものを指定 end def destroy item = Item.find(params[:id]) item.destroy end def edit end def update item = Item.find(params[:id]) item.update(item_params) end def show end def search @items = Item.search(params[:keyword]) end private def item_params params.require(:item).permit(:text, :image ).merge(user_id: current_user.id) #アイテムの名前と写真だけを許可 #mergeメソッドとは2つのハッシュを1つにする end def set_item @item = Item.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end end # 画像の保存を許可するストロングパラメーター 文字数の制限で、質問欄にコードを記載できなかったため、 こちらに記述させていただきました。
winterboum

2020/11/06 13:21

それは controllers の方です。  見たいのは  app/modesl/item.rb のcodeです
yutaro_0529

2020/11/07 06:01

すみません、誤っていました。 こちらが app/modesl/item.rb のcodeになります! class Item < ApplicationRecord validates :text, presence: true #テキストが空だと登録出来ない belongs_to :user has_one_attached :image #画像ファイルをitemテーブルのレコードに添付 #itemテーブルにカラムを追加する必要なし def self.search(search) if search != "" Item.where('text LIKE(?)', "%#{search}%") else Item.all end end end
winterboum

2020/11/07 06:13

S3に置く ためのcodeはどこでしょう
winterboum

2020/11/07 06:19

あ、ActiveStrageですね でしたら url_for(item.image) でどうかな
yutaro_0529

2020/11/07 07:58

はい、ActiveStorageに保存できるように設定していました! url_for(item.image)に変更してみたところまたエラーが出てしまいました。 NoMethodError in Items#index Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line #2 raised: undefined method `url' for #<ActiveStorage::Attached::One:0x00007fbd933243a0> Extracted source (around line #2): 1 2 3 4 5 6 <div class="content_post" > <div class="content_post" style="background-image: url(<%= url_for(item.image.url)%>);"> <div class="more"> <span><%= image_tag 'arrow_top.png'%></span> <ul class="more_list"> <li> Trace of template inclusion: #<ActionView::Template app/views/items/index.html.erb locals=[]> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:2 app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6
winterboum

2020/11/07 08:00

urlではないです url_for です
yutaro_0529

2020/11/07 08:08

すみません、urlを削除してみたところ、SyntaxErrorが発生してしまいました。 ActionView::SyntaxErrorInTemplate in ItemsController#index Encountered a syntax error while rendering template: check <%# <div class="content_post" > %> <div class="content_post" style="background-image: url(<%= url_for(item.image.) <div class="more"> <span><%= image_tag 'arrow_top.png'%></span> <ul class="more_list"> <li> <%= link_to '詳細', item_path(item.id), method: :get%> </li> <% if user_signed_in? && current_user.id == item.user_id %> <li> <%= link_to '削除', item_path(item.id), method: :delete %> </li> <%# Prefixの引数 %> <%# アイテムのID(item.id) %> <%# HTTPメソッドは、DELETEというHTTPメソッド %> <%# methodオプションにシンボルで:delete %> <li> <%= link_to '編集',edit_item_path(item.id), method: :get %> <%# get サーバーからブラウザに情報を返す。単にウェブサイトを閲覧する際など、情報を取得するために利用される。 %> <%# Prefixでパスを確認 %> </li> <% end %> </ul> </div> <p><%= item.text %></p> <span class="name"> <a href="/users/<%= item.user.id %>"> <span>投稿者</span><%= item.user.nickname %> </a> </span> </div> Extracted source (around line #2): 1 2 3 4 5 6 <%# <div class="content_post" > %> <div class="content_post" style="background-image: url(<%= url_for(item.image.) <div class="more"> <span><%= image_tag 'arrow_top.png'%></span> <ul class="more_list"> <li> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 Exception Causes SyntaxError: /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb:2: syntax error, unexpected ')' ...r.append=( url_for(item.image.) ... ^ /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb:4: syntax error, unexpected '<' <span><%= image_tag 'arrow_top... ^ /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' end ^~~ Extracted source (around line #2): 1 2 3 4 5 6 <%# <div class="content_post" > %> <div class="content_post" style="background-image: url(<%= url_for(item.image.) <div class="more"> <span><%= image_tag 'arrow_top.png'%></span> <ul class="more_list"> <li> Rails.root: /Users/uraokayuutarou/projects/nostalgia Application Trace | Framework Trace | Full Trace app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/_item.html.erb:2: syntax error, unexpected ')' app/views/items/_item.html.erb:4: syntax error, unexpected '<' app/views/items/_item.html.erb:35: syntax error, unexpected end, expecting ')' app/views/items/index.html.erb:7 app/views/items/index.html.erb:6:in `each' app/views/items/index.html.erb:6 Request Parameters: None Toggle session dump Toggle env dump Response Headers: None
yutaro_0529

2020/11/07 08:11

_item.html.erb の書き換えをcssを編集すると画像を表示することは出来ました。 <div class="content_post" > <div class="back-image"> <%= image_tag item.image.variant(resize: '350x400'), class: 'item-image' if item.image.attached? %> </div>
winterboum

2020/11/07 09:03

うまく行ったのかな? NGの場合ですが url(<%= url_for(item.image.)   閉じる %> がないです imageのあとにピリオドがある? 不要です
yutaro_0529

2020/11/08 07:43

返信ありがとうございます。 まだうまくいかず、一旦画像の表示だけさせて、 AWSのEC2にデプロイして続きを行おうとしています。 デプロイ後に修正を加えてみます!
yutaro_0529

2020/11/11 08:24

winterboumさんもう少しでデプロイ出来そうです、これが終わったら、 再度取り組みます!!
yutaro_0529

2020/11/13 05:39

winterboumさん、以前のurl(<%= url_for(item.image.)   閉じる %> を実装してみましたが、 再度エラーが出てきたので、これから調べて再度実装して行きます!
winterboum

2020/11/15 12:06

もしかして url(<%= url_for(item.image.)   閉じる %> そのまま書いた? 閉じる %> がないよ、 という意味です
yutaro_0529

2020/11/17 02:16

ご回答頂きありがとうございます! 閉じるは書かず、実装しました! <div class="content_post" style="background-image: url(<%= Rails.application.routes.url_helpers.url_for(item.image) if item.image.attached? %> こちらで実行しましたが、画像は反映されませんでした。
winterboum

2020/11/17 05:24

<div class="content_post" style="background-image: url(<%= url_for(item.image) %>) ;">
yutaro_0529

2020/11/17 05:48

上記のコードで実装させて頂き画像が背景に表示されるようになりました!! ありがとうございます!! 解決しました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問