LoginSignup
22
13

More than 5 years have passed since last update.

active storageの画像がリンク切れする

Last updated at Posted at 2018-02-04

問題

Rails 5.2.0.beta2で開発中、何故か画像がリンク切れする。
コードはドキュメント通り書いている。画像も指定の場所に上がっている。

app/views/accounts/show.html.slim
= image_tag url_for(@account.avatar)

原因

route.rb でのエラー処理の記述が問題だった。
route.rbの一番最後に、以下のように記述して、エラーページを表示していた。

config/route.rb
Rails.application.routes.draw do
  ...
  get "*path", controller: 'application', action: 'error404', via: :all
end

ところが、ActiveStrageは画像のリンクを、以下のように見ているらしい。

rails/activestorage/config/routes.rb
Rails.application.routes.draw do
  get "/rails/active_storage/blobs/:signed_id/*filename" => "active_storage/blobs#show", as: :rails_service_blob, internal: true
  ...
end

project/config/route.rbrails/activestorage/config/routes.rbよりも先に呼ばれるので、画像を呼ぶ前に、エラーページの方を呼んでしまう。

解決方法

エラーハンドリングに関する別の問題(上記のrouteでの記述では、RoutingErrorをキャッチできない)の記事を参考にした。
それと同じ方法でエラー処理するようにしたら解決した。

以下のファイルを作成して、route.rbの記述を削除。サーバを再起動する。

config/initilizers/exceptions_app.rb
Rails.application.configure do
  config.exceptions_app = ErrorsController.action(:show)
end
app/controllers/error_controller.rb
class ErrorsController < ApplicationController

  skip_after_action :verify_authorized

  layout 'application'

  rescue_from StandardError, with: :error500
  rescue_from ActionController::RoutingError, with: :error404
  rescue_from ActiveRecord::RecordNotFound, with: :error404

  def error404(exception = nil)
    logger.info "Rendering 404 with exception: #{exception.message}" if exception
    render template: 'errors/error404', status: 404
  end

  def error500(exception = nil)
    logger.info "Rendering 500 with exception: #{exception.message}" if exception
    render template: 'errors/error500', status: 500
  end

  def show
   raise
  end
end

詳細は以下の参考のリンクから。

参考

22
13
1

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
22
13