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

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

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

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

Q&A

解決済

1回答

3377閲覧

投稿したプロトタイプがトップページで表示されるようにしたい

0W5E8fPq1EOm4yE

総合スコア13

Ruby on Rails 6

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

0グッド

1クリップ

投稿2020/11/23 13:17

編集2020/11/24 11:59

前提・実現したいこと

テーブルに保存されているすべてのプロトタイプが、トップページに表示されるようにしたいのですがSprockets::Rails::Helper::AssetNotFound in Prototypes#indexとエラーになってしまいました。

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

Sprockets::Rails::Helper::AssetNotFound in Prototypes#index

該当のソースコード

_prototype.html.erb <div class="card"> <%= link_to image_tag("プロトタイプの画像", class: :card__img ), root_path%> <div class="card__body"> <%= link_to "プロトタイプのタイトル", root_path, class: :card__title %> <%= prototype.title %> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by プロトタイプの投稿者名", root_path, class: :card__user %> <%= prototype.user.name %> </div> </div>
config.rotes Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy] end
prototype_controller.rb class PrototypesController < ApplicationController #CSRF保護を無効にする protect_from_forgery with: :null_session before_action :move_to_index, except: [:index, :show] def index @prototype = Prototype.all.includes(:user) end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new @prototype = Prototype.includes(:user) end end def show @prototype = Prototype.find(params[:id]) end def edit @prototype = Prototype.find(params[:id]) end def update prototype = prototype.find(params[:id]) prototype.update(prototype_params) if prototype.save redirect_to root_path(@prototype) else @prototype = @user.prototype.includes(:user) render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params[:prototype].permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index end end end
ターミナル app/views/prototypes/_prototype.html.erb:2 app/views/prototypes/index.html.erb:11 Started GET "/" for ::1 at 2020-11-24 20:58:09 +0900 Processing by PrototypesController#index as HTML Rendering prototypes/index.html.erb within layouts/application User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/views/prototypes/index.html.erb:4 Rendered prototypes/_prototype.html.erb (Duration: 13.8ms | Allocations: 5048) Rendered prototypes/index.html.erb within layouts/application (Duration: 16.0ms | Allocations: 6122) Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.4ms | Allocations: 7008) ActionView::Template::Error (The asset "プロトタイプの画像" is not present in the asset pipeline. ): 1: <div class="card"> 2: <%= link_to image_tag("プロトタイプの画像", class: :card__img ), root_path %> 3: 4: <div class="card__body"> 5: <%= link_to "プロトタイプのタイトル", root_path, class: :card__title %> app/views/prototypes/_prototype.html.erb:2

試したこと

<%= link_to image_tag("プロトタイプの画像", class: :card__img ), root_path%>
から
<%= link_to image_tag(prototype.image, class: :card__img ), root_path%>に変更したがNo methodエラーとなった。イメージ説明

補足情報(FW/ツールのバージョンなど)

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

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

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

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

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

m.ts10806

2020/11/23 21:20

コードブロックはファイル毎にわけてください。 混在していると再現確認の際に色んな対応が必要になって、非常に面倒です。
0W5E8fPq1EOm4yE

2020/11/24 09:50

コードをファイル毎に分けました!
guest

回答1

0

ベストアンサー

エラーメッセージで検索すると、以下の記事がヒットします。
sprockets::rails::helper::assetnotfound エラーの原因

app/assets/images 以下にプロトタイプの画像というファイルが存在しますか?

prototype.imageとした時にエラーとなるのは、app/views/prototypes/index.html.erb から
パーシャル(_prototype.html.erb)が適切に呼ばれていないのかもしれません。

投稿2020/11/24 11:58

neko_daisuki

総合スコア2090

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

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

0W5E8fPq1EOm4yE

2020/11/24 12:15

ありがとうございます。今度は以下のURLのようなNoMethodErrorが出てしまいました。 https://gyazo.com/972cdc6ae8cbf595b6eb62b55b1e05b7 ターミナル app/views/prototypes/_prototype.html.erb:6 app/views/prototypes/index.html.erb:11 Started GET "/" for ::1 at 2020-11-24 21:11:43 +0900 Processing by PrototypesController#index as HTML Rendering prototypes/index.html.erb within layouts/application User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/views/prototypes/index.html.erb:4 Prototype Load (0.3ms) SELECT `prototypes`.* FROM `prototypes` LIMIT 11 ↳ app/views/prototypes/index.html.erb:11 Rendered prototypes/_prototype.html.erb (Duration: 7.9ms | Allocations: 4608) Rendered prototypes/index.html.erb within layouts/application (Duration: 11.5ms | Allocations: 6000) Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.7ms | Allocations: 7720) ActionView::Template::Error (undefined method `title' for #<Prototype::ActiveRecord_Relation:0x00007fba9b4b5b60>): 3: 4: <div class="card__body"> 5: <%= link_to "プロトタイプのタイトル", root_path, class: :card__title %> 6: <%= prototype.title.id %> 7: 8: <p class="card__summary"> 9: <%= prototype.catch_copy %> app/views/prototypes/_prototype.html.erb:6 app/views/prototypes/index.html.erb:11
neko_daisuki

2020/11/24 12:51

prototype が ActiveRecord_Relation なのは変です。 prototypes/index.html.erb に _prototype.html を render しているところがあると思うのですが、 その部分を追記してください。
0W5E8fPq1EOm4yE

2020/11/24 13:03

直りました!どうもありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問