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

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

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

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

Q&A

解決済

1回答

1201閲覧

記事詳細画面において、画像が表示されない。Sprockets::Rails::Helper::AssetNotFound

tom000

総合スコア2

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/14 02:06

現状

railsにてブログアプリを作成中。記事の登録と同時に画像が登録されるようにfields_forを使用しています。フォームから送信した後、データベースには保存されているのですが、下記のようにエラーが出てしまいます。

Sprockets::Rails::Helper::AssetNotFound in Articles#show
The asset "ゴルフボール.png" is not present in the asset pipeline.

実現したいこと

エラーを解決し、記事詳細画面に画像を表示させたい。

実装環境

ruby 2.7.1
Rails 6.0.3.4

carrierwave
mini_magick

コード

show.html.haml

haml

1%h1 記事詳細ページ 2 3= @article.title 4= @article.created_at 5= image_tag @article.images[0].src.to_s 6= simple_format @article.content

articles.controller.rb

ruby

1class ArticlesController < ApplicationController 2 before_action :set_article, only: [:show, :edit] 3 before_action :move_to_index, except: [:index, :show, :search] 4 5 def index 6 @articles = Article.includes(:user) 7 end 8 9 def new 10 @article = Article.new 11 @article.images.new 12 end 13 14 def create 15 Article.create(article_params) 16 redirect_to root_path 17 end 18 19 def show 20 @comment = Comment.new 21 @comments = @article.comments.includes(:user) 22 end 23 24 def update 25 article = Article.find(params[:id]) 26 article.update(article_params) 27 redirect_to article_path(article.id) 28 end 29 30 private 31 def article_params 32 params.require(:article).permit(:title, :content, images_attributes: [:src]).merge(user_id: current_user.id) 33 end 34 35 def set_article 36 @article = Article.find(params[:id]) 37 end 38 39 def move_to_index 40 redirect_to action: :index unless user_signed_in? 41 end 42 43end 44

routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root 'articles#index' 4 resources :articles, except: :index do 5 resources :comments, only: [:create, :edit, :update, :destroy] 6 collection do 7 get 'search' 8 end 9 end 10 11 resources :users, only: :show 12 13end

iamge.rb

ruby

1class Image < ApplicationRecord 2 mount_uploader :src, ImageUploader 3 belongs_to :article 4end

image_uploader.rb

ruby

1class ImageUploader < CarrierWave::Uploader::Base 2 # Include RMagick or MiniMagick support: 3 # include CarrierWave::RMagick 4 include CarrierWave::MiniMagick 5 6 # Choose what kind of storage to use for this uploader: 7 storage :file 8 # storage :fog 9 10 # Override the directory where uploaded files will be stored. 11 # This is a sensible default for uploaders that are meant to be mounted: 12 def store_dir 13 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 14 end 15 16 # Provide a default URL as a default if there hasn't been a file uploaded: 17 # def default_url(*args) 18 # # For Rails 3.1+ asset pipeline compatibility: 19 # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 20 # 21 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 22 # end 23 24 # Process files as they are uploaded: 25 # process scale: [200, 300] 26 # 27 # def scale(width, height) 28 # # do something 29 # end 30 31 # Create different versions of your uploaded files: 32 # version :thumb do 33 # process resize_to_fit: [50, 50] 34 # end 35 36 # Add a white list of extensions which are allowed to be uploaded. 37 # For images you might use something like this: 38 # def extension_whitelist 39 # %w(jpg jpeg gif png) 40 # end 41 42 # Override the filename of the uploaded files: 43 # Avoid using model.id or version_name here, see uploader/store.rb for details. 44 # def filename 45 # "something.jpg" if original_filename 46 # end 47 48 process resize_to_fit: [100, 100] 49 50end

試したこと

public/uploadsを確認したところ、画像ファイルが保存されておらず、これが原因だと考えているのですが、image_uploader関連の記述は記載しており、問題点を見つけることができませんでした。

何かアドバイスなど頂けると幸いです。よろしくお願い致します。

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

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

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

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

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

guest

回答1

0

自己解決

自己解決しましたので報告します。
bundle installをしてサーバーを再起動していないことが原因でした。
ありがとうございました。

投稿2020/11/14 02:15

tom000

総合スコア2

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問