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

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

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

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

Q&A

1回答

585閲覧

Railsであるモデルをnewするとinvalid byte sequence in UTF-8と怒られる

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails

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

0グッド

1クリップ

投稿2017/11/22 16:41

###あるモデルをnewした時にinvalid byte sequence in UTF-8といわれます

ecサイトを作成しています

Staffのnamespace配下の全部で20ほどあるコントローラーの内、一つの挙動が異なっているようです
staff/marchandiseというコントローラーのnewアクションでmarchandiseモデルをnewすると

ArgumentError in Staff::MarchandiseController#new(標準のエラー画面である赤い帯に白字。このエラーだとなぜかbetter_errorの画面にならない)
invalid byte sequence in UTF-8
と表示されます

スタッフメンバーがmarchandiseを登録するという設計で、newへのリンクを押すとこのエラーが表示されます
リレーションはstaff_member 1:n marchandise です

よくあるユーザーと投稿の組み合わせをイメージしてもらえればと思います

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

コンソールには以下が表示されています I, [2017-11-23T00:55:42.774069 #14189] INFO -- : Completed 500 Internal Server Error in 27342ms (ActiveRecord: 1.4ms) F, [2017-11-22T22:34:31.940160 #81045] FATAL -- : F, [2017-11-22T22:34:31.940389 #81045] FATAL -- : ArgumentError (invalid byte sequence in UTF-8): F, [2017-11-22T22:34:31.940475 #81045] FATAL -- : F, [2017-11-22T22:34:31.940560 #81045] FATAL -- : invalid byte sequence in UTF-8 better_errors (2.1.1) lib/better_errors/raised_exception.rb:59:in `massage_syntax_error' better_errors (2.1.1) lib/better_errors/raised_exception.rb:15:in `initialize' better_errors (2.1.1) lib/better_errors/error_page.rb:19:in `new' better_errors (2.1.1) lib/better_errors/error_page.rb:19:in `initialize' better_errors (2.1.1) lib/better_errors/middleware.rb:86:in `new' better_errors (2.1.1) lib/better_errors/middleware.rb:86:in `rescue in protected_app_call' better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call' better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call' better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call' web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app' web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call' web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch' web-console (3.5.1) lib/web_console/middleware.rb:18:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call' railties (5.1.4) lib/rails/rack/logger.rb:36:in `call_app' railties (5.1.4) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/remote_ip.rb:79:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/request_id.rb:25:in `call' rack (2.0.3) lib/rack/method_override.rb:22:in `call' rack (2.0.3) lib/rack/runtime.rb:22:in `call' activesupport (5.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/executor.rb:12:in `call' actionpack (5.1.4) lib/action_dispatch/middleware/static.rb:125:in `call' rack (2.0.3) lib/rack/sendfile.rb:111:in `call' rack-mini-profiler (0.10.5) lib/mini_profiler/profiler.rb:282:in `call' railties (5.1.4) lib/rails/engine.rb:522:in `call' puma (3.10.0) lib/puma/configuration.rb:225:in `call' puma (3.10.0) lib/puma/server.rb:605:in `handle_request' puma (3.10.0) lib/puma/server.rb:437:in `process_client' puma (3.10.0) lib/puma/server.rb:301:in `block in run' puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

###該当のソースコード
marchandise_controller.rb

marchandise_controller.rb

1def new 2 # binding.pry 3 @marchandise = Marchandise.new 4end

###試したこと
このバイトはUTF-8では無効なシーケンスだぜ。と言われているのは理解できていますが、どれがかはわかりませんでした。

元々この部分を担当していたのはProductという名前でしたが、なにがよくないのか分からないのでMarchandiseに変更して一から打ち直しています

このエラーではbetter_errorの画面にならないのでbetter_errorsをコメントアウトして再試行すると
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.(Herokuでおなじみの画面)といわれ、このアクション内で別のモデルをnewしたところ、正常にnewされているのでこのコントローラー自体が変ということではなさそうでした

ただし、他のコントローラーでMarchandiseをnewすると同様のエラーが起きるのでmarchandiseの中身が変なのかなと思います

marchandiseのmigrateはこちら

class CreateMarchandises < ActiveRecord::Migration[5.1] def change create_table :marchandises do |t| t.references :staff_member, foreign_key: true t.references :belt, foreign_key: true t.references :coat, foreign_key: true t.references :cuff_link, foreign_key: true t.references :ear_muffler, foreign_key: true t.references :gant, foreign_key: true t.references :hat, foreign_key: true t.references :jacket, foreign_key: true t.references :knit, foreign_key: true t.references :lapel_pin, foreign_key: true t.references :muffler, foreign_key: true t.references :other, foreign_key: true t.references :pant, foreign_key: true t.references :pocket_chief, foreign_key: true t.references :shoe, foreign_key: true t.references :shurt, foreign_key: true t.references :sock, foreign_key: true t.references :tie_neck, foreign_key: true t.references :tie_pin, foreign_key: true t.references :vest, foreign_key: true t.string :product_face t.string :code t.integer :scene t.integer :season t.text :description t.integer :price t.timestamps end end end

###補足情報(言語/FW/ツール等のバージョンなど)
Ruby 2.4.1
Rails 5.1.4
postgreSQL 9.6.3

echo $LANG ja_JP.UTF-8

よろしくお願いいたします

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

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

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

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

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

maisumakun

2017/11/22 22:21

当該のコントローラーファイルをバイナリダンプで確認してみてください。なにか変なデータが入っていたりはしないでしょうか。
退会済みユーザー

退会済みユーザー

2017/11/23 05:01

ありがとうございます。確認してみたところ、とくに変わった様子はありませんでした。
guest

回答1

0

文字コードにあまりきちんと配慮していない better_errors の欠陥ですが,エラーが起こったときにそのエラーメッセージの文字列を何かしようとして,better_errors 自身が例外を吐くことがあります。(UTF-8 じゃないのに UTF-8 と決め打ちしている)
このケースもそれかもしれません。

We're sorry, but something went wrong. が出たのであれば,その次に書かれているとおり,ログファイルをまず見るといいと思います。
log/development.loglog/production.log

投稿2017/11/28 10:40

scivola

総合スコア2108

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問