RuntimeErrorで画像投稿ができなかった場合の対処法


背景

Refileなどの画像投稿が出来るgemを導入し、画像を実際に格納してみたら
RuntimeErrorとされた。よくあることらしいので備忘録も含めて。

使用環境
ruby 2.7.2 Rails 6.0.3.4

対処法

エラー文をよく読むとそこまで難しくない。そのままの通り実行することする。

RuntimeError at / Refile.secret_key was not set.

Please add the following to your Refile configuration and restart your application:

Refile.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

このRefile.secret_key='XXX'をそのままコピー。
時間を置くとkeyが変わるので注意。

/config/initializers/application_controller_renderer.rb
# Be sure to restart your server when you modify this file.

# ActiveSupport::Reloader.to_prepare do
#   ApplicationController.renderer.defaults.merge!(
#     http_host: 'example.org',
#     https: false
#   )
# end

#以下を貼り付ける
Refile.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

Railsを再起動し、リロードし直すと無事画像が投稿されたのが確認された。