Rails で画像を投稿できるアプリを開発中なのですが、
画像をアップロードしようとすると下記のエラーが出ます。
Sprockets::Rails::Helper::AssetNotFound in Posts#index
The asset "" is not present in the asset pipeline.
userテーブルにはprofile_photoカラムを追加しており、
nilの場合、アバターの画像を返す様に設定しております。
どの部分でエラーが起きているのか判断できず、恐れ入りますがお分かりの方は
ご回答いただけますでしょうか。
★posts/index.hjtml.erbのコード
Rails
1<% provide(:title, "My Posts") %> 2 3<div class="row"> 4<% @posts.each do |post| %> 5 6 <div class="col-md-3 mx-auto"> 7 <div class="card-wrap"> 8 <div class="card"> 9 <div class="card-header align-items-center d-flex"> 10 <%= link_to user_path(post.user), class: "no-text-decoration" do %> 11 <%= image_tag avatar_url(post.user), class: "post-profile-icon" %> 12 <% end %> 13 <%= link_to user_path(post.user), class: "black-color no-text-decoration", 14 title: post.user.name do %> 15 <strong><%= post.user.name %></strong> 16 <% end %> 17 18 <% if post.user_id == current_user.id %> 19 <%= link_to post_path(post), method: :delete, class: "ml-auto mx-0 my-auto" do %> 20 <div class="delete-post-icon"> 21 </div> 22 <% end %> 23 <% end %> 24 25 </div> 26 27 <%= link_to(post_path(post)) do %> 28 <%= image_tag post.photos.first.image.url(:medium), class: "card-img-top" %> 29 <% end %> 30 31 <div class="card-body"> 32 <div class="row parts"> 33 34 <div id="like-icon-post-<%= post.id.to_s %>"> 35 <% if post.liked_by(current_user).present? %> 36 <%= link_to "いいねを取り消す", post_like_path(post.id, post.liked_by(current_user)), method: :DELETE, remote: true, class: "loved hide-text" %> 37 <% else %> 38 <%= link_to "いいね", post_likes_path(post), method: :POST, remote: true, class: "love hide-text" %> 39 <% end %> 40 </div> 41 42 <%= link_to "", "#", class: "comment" %> 43 </div> 44 45 <div> 46 <span><strong><%= post.user.name %></strong></span> 47 <span><%= post.caption %></span> 48 <%= link_to time_ago_in_words(post.created_at).upcase + "前", post_path(post), class: "post-time no-text-decoration" %> 49 <hr> 50 51 52 <div id="comment-post-<%= post.id.to_s %>"> 53 <%= render 'comment_list', { post: post } %> 54 </div> 55 <%= link_to time_ago_in_words(post.created_at).upcase + "前", post_path(post), 56 class: "light-color post-time no-text-decoration" %> 57 58 59 <div class="row actions" id="comment-form-post-<%= post.id.to_s %>"> 60 <%= form_with model: [post, Comment.new], class: "w-100" do |f| %> 61 <%= f.hidden_field :user_id, value: current_user.id %> 62 <%= f.hidden_field :post_id, value: post.id %> 63 <%= f.text_field :comment, class: "form-control comment-input border-0", placeholder: "コメント ...", autocomplete: :off %> 64 <% end %> 65 </div> 66 </div> 67 </div> 68 </div> 69 </div> 70 </div> 71<% end %> 72<code>
★postコントローラ
Rails5.2
1class PostsController < ApplicationController 2 before_action :authenticate_user! 3 before_action :set_post, only: %i(show destroy) 4 5 6 def index 7 @posts = Post.all.includes(:photos, :user).order('created_at DESC') 8 end 9 10 def show 11 @post = Post.find_by(id: params[:id]) 12 end 13 14 def new 15 @post = Post.new 16 @post.photos.build 17 end 18 19 def create 20 @post = Post.new(post_params) 21 if @post.photos.present? 22 @post.save 23 redirect_to "/posts" 24 flash[:notice] = "投稿が保存されました" 25 else 26 redirect_to root_path 27 flash[:alert] = "投稿に失敗しました" 28 end 29 end 30 31 32 def destroy 33 if @post.user == current_user 34 flash[:notice] = "投稿が削除されました" if @post.destroy 35 else 36 flash[:alert] = "投稿の削除に失敗しました" 37 end 38 redirect_to root_path 39 end 40 41 private 42 def post_params 43 params.require(:post).permit(:caption, photos_attributes: [:image]).merge(user_id: current_user.id) 44 end 45 46 def set_post 47 @post =Post.find_by(id: params[:id]) 48 end 49end 50
★application.helperでのアバターの設定
Rails
1module ApplicationHelper 2 3 def full_title(page_title = '') 4 base_title = "Instagram" 5 if page_title.empty? 6 base_title 7 else 8 page_title + " | " + base_title 9 10 end 11 end 12 13 def avatar_url(user) 14 return user.profile_photo unless user.profile_photo.nil? 15 gravatar_id = Digest::MD5::hexdigest(user.email).downcase 16 "https://www.gravatar.com/avatar/#{gravatar_id}.jpg" 17 end 18end 19 20
回答1件
あなたの回答
tips
プレビュー