0
0

More than 3 years have passed since last update.

検索機能(画像も含む)でローカル上は正常、heroku上ではエラー 解決策

Posted at

検索機能(画像含む)がある、簡単な投稿サイトポートフォリオを作成中に起きたエラー。
今回かなり時間を費やしてしまい備忘録のためと、他に困っている方などいましたらご参考までにして頂けたら幸いです!

プログラミング学習始め日が浅いため、コード等ではもっと上手な記述の仕方がありました、合わせてご指摘も頂けましたら幸いです。

今回発程したエラーはローカル上では検索機能(画像含む)が正常に動いているが、
herokuでデプロイした際に検索ボタンを押すと、エラーになるといった内容。

ransackのjemを導入済み。

始めに確認したことは、ターミナルでエラーログを確認。

ターミナル

2021-03-19T05:26:08.790107+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86] ActionView::Template::Error (Nil location provided. Can't build URI.):
2021-03-19T05:26:08.790108+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]      7:       <div class='list'>
2021-03-19T05:26:08.790108+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]      8:         <%= link_to post_path(post.id), method: :get do %>
2021-03-19T05:26:08.790109+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]      9:         <div class='item-img-content'>
2021-03-19T05:26:08.790130+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]     10:           <td><%= image_tag post.images.first, class: "item-img" %></td>
2021-03-19T05:26:08.790131+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]     11:         </div>
2021-03-19T05:26:08.790132+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]     12:         <div class="names">
2021-03-19T05:26:08.790132+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]     13:           <td><%= post.name %></td>
2021-03-19T05:26:08.790133+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86]
2021-03-19T05:26:08.790133+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86] app/views/posts/search.html.erb:10
2021-03-19T05:26:08.790134+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86] app/views/posts/search.html.erb:8
2021-03-19T05:26:08.790134+00:00 app[web.1]: [3211f911-070c-4c95-9858-435d1ef98f86] app/views/posts/search.html.erb:6
2021-03-19T05:26:08.792945+00:00 heroku[router]: at=info method=GET path="/posts/search?q%5Bname_or_post_text_cont%5D=%E3%81%82&button=" host=bikerlife-989.herokuapp.com request_id=3211f911-070c-4c95-9858-435d1ef98f86 fwd="126.210.2.214" dyno=web.1 connect=1ms service=44ms status=500 bytes=1827 protocol=https
2021-03-19T05:26:13.333553+00:00 app[web.1]: I, [2021-03-19T05:26:13.333443 #4]  INFO -- : [c5923adb-0fb2-472e-bdc2-9f1a2aa2e4c6] Started GET "/posts/search?q%5Bname_or_post_text_cont%5D=&button=" for 126.210.2.214 at 2021-03-19 05:26:13 +0000

サーバーサイド側の500エラーが出てると認識。

エラーを読みとくに、一行目の

ActionView::Template::Error (Nil location provided. Can't build URI.):

上記記述に着目した。
提供されている画像がないということがエラーの原因と突き止めた。

検索機能のコード
search.html.erb

<%= render "shared/seconde_header3" %>

<div class='intermediatesse'>
  <h2 class="intermediates_head">検索結果</h2>
  <div class='item-lists'>
    <% @results.each do |post| %>
      <div class='list'>
        <%= link_to post_path(post.id), method: :get do %>
        <div class='item-img-content'>
          <td><%= image_tag post.images.first, class: "item-img" %></td>
        </div>
        <div class="names">
          <td><%= post.name %></td>
        </div>
        <% end %>
      </div>
    <% end %>
  </div>
</div>

<%= render "shared/footer" %>

posts_controller

class PostsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_q, only: [:index, :search]
  before_action :set_item, only: [:edit, :show, :update, :destroy]
  before_action :redirect, only: [:edit, :destroy]

  def index
    @posts = Post.includes(:user).page(params[:page]).per(9).order("created_at DESC")
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to posts_path
    else
      render :new
    end
  end

  def show
    @comment = Comment.new
    @comments = @post.comments.includes(:user).order("created_at DESC")
  end

  def edit
  end

  def update
    return redirect_to posts_path if @post.user != current_user
    if @post.update(post_params)
      redirect_to post_path(@post.id)
    else
      render :edit
    end
  end

  def destroy
    @post.destroy
    redirect_to posts_path
  end

  def search
    @results = @q.result
  end

  private

  def post_params
    params.require(:post).permit(:name, :post_text, images: []).merge(user_id: current_user.id)
  end

  def set_q
    @q = Post.ransack(params[:q])
  end

  def set_item
    @post = Post.find(params[:id])
  end

  def redirect
    redirect_to posts_path if @post.user != current_user
  end
end

post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :images
  has_many :comments, foreign_key: :post_id, dependent: :destroy
  has_many :likes

  with_options presence: true do
    validates :name
    validates :images
  end
end

今回は複数枚画像を投稿できるようにしている状態。

画像がないということだったので、シークエルプロで画像情報を確認。
画像の保存に関しては、mini_magickとimage_processingのjemを導入している。

問題なく画像保存ができており、なんでだろうと模索する。

そこで気がついたのはローカル上の画像とデプロイ上の画像の保存場所は違うと気づく。
本番環境ではS3を導入していないため24時間で画像が削除されている状態。

そこがエラーの原因であった。

S3の導入もしていなく、24時間経っているのであれば、画像は削除されていないはず、そこに気づけた。

エラーに気づけた要因はシークエルプロである。
シークエルプロと睨めっこしていてなぜかと自分に問い続けたら、冷静に開発環境と本番環境の保存場所が違うことに気づけたといった流れである。

原因さえわかれば、あとはその原因を解消するだけ!

そこでherokuデプロイ後に投稿していた画像データ全てをリセットする、コマンドをターミナルで実行する。

heroku run DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:drop db:create db:migrate

上記コマンドを行い、本番環境のデータベースを全てリセットした。

再度投稿を行い、検索機能を試してみると問題なく作動した!

このエラーで改めて開発環境と本番環境での違いを認識することができた。

このようなエラーで他にも困っている方などいましたらご参考にして頂けましたら幸いです。

コードの記述でも可読性の良い記述方法等ありましたらアドバイス頂けましたら幸いです。

宜しくお願いします!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0