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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

1回答

1526閲覧

deviseでパスワード認証を行うとエラーが発生する

shohei_mlb

総合スコア9

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/24 10:38

編集2021/10/30 02:29

■環境
OS: macOS Big Sur(M1 mac) 11.5.2
Ruby: 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin20]
Rails: 6.1.4.1

■背景
railsでパスワードの暗号化を行うためにgemのbcryptをインストールしたが、
おそらくM1 macの環境下では使えないという問題が発生したため、**「bcrypt」の代わりに「devise」**を使うことにした。
結果、deviseでもbcrypt実装時と似たエラーが発生し、何をどう直せばよいか良いかわからない状況です。

■bcryptがM1 Macで使えないと判断した記事
https://github.com/bcrypt-ruby/bcrypt-ruby/issues/239
https://teratail.com/questions/318921

■実現したいこと
メールとパスワードを入力し、ユーザーの新規登録を行うために、下記のエラー内容を解決したい。

■エラーが出るまでの流れ

  1. railsにdeviseをインストール
  2. 新規登録画面からメールとパスワードを入力
  3. 新規登録ボタンを押下すると、以下のエラーが発生する

■エラー内容

ruby

1Started GET "/users/sign_up" for 127.0.0.1 at 2021-10-25 09:45:00 +0900 2Processing by Devise::RegistrationsController#new as HTML 3 Rendering layout layouts/application.html.erb 4 Rendering users/registrations/new.html.erb within layouts/application 5 Rendered users/shared/_error_messages.html.erb (Duration: 0.9ms | Allocations: 223) 6 Rendered users/shared/_links.html.erb (Duration: 1.2ms | Allocations: 533) 7 Rendered users/registrations/new.html.erb within layouts/application (Duration: 13.3ms | Allocations: 5339) 8[Webpacker] Everything's up-to-date. Nothing to do 9 Rendered layout layouts/application.html.erb (Duration: 21.0ms | Allocations: 10257) 10Completed 200 OK in 45ms (Views: 25.2ms | ActiveRecord: 1.2ms | Allocations: 20580) 11 12 13Started POST "/users" for 127.0.0.1 at 2021-10-25 09:45:40 +0900 14Processing by Devise::RegistrationsController#create as HTML 15 Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"user_name"=>"test9999", "email"=>"test9999@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 16Completed 500 Internal Server Error in 28ms (ActiveRecord: 0.0ms | Allocations: 6201) 17 18 19 20LoadError - dlopen(/Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle, 9): no suitable image found. Did find: 21 /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: mach-o, but wrong architecture 22 /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: mach-o, but wrong architecture - /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: 23 24Started POST "/__better_errors/30db173a3c039884/variables" for 127.0.0.1 at 2021-10-25 09:45:40 +0900 25Since there is no EDITOR or BETTER_ERRORS_EDITOR environment variable, using Textmate by default.

■補足
「devise (4.8.0)」はインストール済みで、Gemfileにデフォルトで記載されてある、
「gem 'bcrypt', '~> 3.1.7'」はコメントアウトされたままだが、「bcrypt (3.1.16)」はインストールされている。
→おそらく、deviseインストール時に自動でbcryptがインストールされたと思われます。

■Gemfile

Ruby

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '3.0.0' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.4', '>= 6.1.4.1' 8# Use sqlite3 as the database for Active Record 9gem 'sqlite3', '~> 1.4' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24# gem 'bcrypt-ruby', '3.1.5', :require => 'bcrypt' 25 26# Use Active Storage variant 27# gem 'image_processing', '~> 1.2' 28 29# Reduces boot times through caching; required in config/boot.rb 30gem 'bootsnap', '>= 1.4.4', require: false 31 32gem 'devise' 33 34group :development, :test do 35 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 36 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 37end 38 39group :development do 40 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 41 gem 'web-console', '>= 4.1.0' 42 # Display performance information such as SQL time and flame graphs for each request in your browser. 43 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 44 gem 'rack-mini-profiler', '~> 2.0' 45 gem 'listen', '~> 3.3' 46 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 47 gem 'spring' 48 gem 'better_errors' 49 gem 'binding_of_caller' 50end 51 52group :test do 53 # Adds support for Capybara system testing and selenium driver 54 gem 'capybara', '>= 3.26' 55 gem 'selenium-webdriver' 56 # Easy installation and use of web drivers to run system tests with browsers 57 gem 'webdrivers' 58end 59 60# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 61gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 62

■やったこと1
「/Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle」を削除したが、
同様のエラーが発生した。

■やったこと2
「rbenv uninstall bcrypt」を実行したが、「rbenv: version `bcrypt' not installed」となり、
bcryptはインストールされていなかった。

以上、ご教授いただけますと幸いです。

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

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

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

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

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

shohei_mlb

2021/10/24 16:12

エラー時に出てくるメッセージは上記に加えて、下記のみになります。 Started POST "/__better_errors/6ab9d2b12f9dac3d/variables" for 127.0.0.1 at 2021-10-25 01:02:40 +0900 Since there is no EDITOR or BETTER_ERRORS_EDITOR environment variable, using Textmate by default.
MasaSakano

2021/10/24 16:25

これは、コンソールへの出力ですね、おそらく? `log/development.log` などにもっと詳しい情報が出力されていると思うのですが。行番号などの。 それとは別に、`rbenv uninstall bcrypt` でシステムから bcrypt をアンインストールすると状況が変わるかも知れないとは思いました。
shohei_mlb

2021/10/25 00:56

これは、コンソールへの出力ですね、おそらく? `log/development.log` などにもっと詳しい情報が出力されていると思うのですが。行番号などの。 > development.logを確認し、コンソールに出力されているエラー文と変わりませんでしたが、 念の為、全文を載せておきます。 Started GET "/users/sign_up" for 127.0.0.1 at 2021-10-25 09:45:00 +0900 Processing by Devise::RegistrationsController#new as HTML Rendering layout layouts/application.html.erb Rendering users/registrations/new.html.erb within layouts/application Rendered users/shared/_error_messages.html.erb (Duration: 0.9ms | Allocations: 223) Rendered users/shared/_links.html.erb (Duration: 1.2ms | Allocations: 533) Rendered users/registrations/new.html.erb within layouts/application (Duration: 13.3ms | Allocations: 5339) [Webpacker] Everything's up-to-date. Nothing to do Rendered layout layouts/application.html.erb (Duration: 21.0ms | Allocations: 10257) Completed 200 OK in 45ms (Views: 25.2ms | ActiveRecord: 1.2ms | Allocations: 20580) Started POST "/users" for 127.0.0.1 at 2021-10-25 09:45:40 +0900 Processing by Devise::RegistrationsController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"user_name"=>"test9999", "email"=>"test9999@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} Completed 500 Internal Server Error in 28ms (ActiveRecord: 0.0ms | Allocations: 6201) LoadError - dlopen(/Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle, 9): no suitable image found. Did find: /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: mach-o, but wrong architecture /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: mach-o, but wrong architecture - /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle: Started POST "/__better_errors/30db173a3c039884/variables" for 127.0.0.1 at 2021-10-25 09:45:40 +0900
shohei_mlb

2021/10/25 00:57

それとは別に、`rbenv uninstall bcrypt` でシステムから bcrypt をアンインストールすると状況が変わるかも知れないとは思いました。 > 「rbenv uninstall bcrypt」を実行しましたが、「rbenv: version `bcrypt' not installed」となり、 bcryptはインストールされていませんでした。
MasaSakano

2021/10/25 01:27

まず、本サイトにおいて、コメント欄は、質問への回答を書く場所では「ありません」。コメントは、あくまで質問を「改善する」ための助けとして使います。コメントで得られた知見をもとにして、元々の質問を編集し、より明快で必要十分な情報がある質問となるように改善して下さい(編集すれば、デフォルトで質問者へも通知がいきます。つまり、コメント欄を使って私に回答する必要はそもそもありません)。今後、同様の疑問をもって、ここを検索して調べる人のことを考えてみればなぜそういうポリシーなのかは理解できるでしょう。最良の質問と最高の回答を他の人々のために残すことこそが、このサイトの目的と考えていいと思います。 さて、ターミナルでrbenv version を実行した出力は、3.0.0 でしょうか。もしそうでなければ、uninstallの前に rbenv shell 3.0.0 を実行する必要があります。
guest

回答1

0

Gemの依存関係、あるいはライブラリの優先順位がおかしなことになっている様子です。普通は、報告のような状況にならないはずなので。
以下、何がおかしいかという推測(というか憶測)、および私ならばこれをトライする、という解決の指針の提案です。

まず、devise は、bcrypt に依存します。だから、deviseをインストールした時に bcryptもインストールされること自体は正当です。その時、Railsでdeviseをインストールしたのである限りは、bcryptがインストールされるディレクトリは、 あなたのRailsのトップ/vendor/bundle/ruby/3.0.0/gems/bcrypt-3.1.16/ などであるはずです(バージョン番号は一例)。

しかし、ご報告では、 /Users/my_name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.16/lib/bcrypt_ext.bundle についてエラーが出ています。これはRailsアプリのディレクトリではなく、お使いのRubyのシステムディレクトリです。このディレクトリをRails/Deviseが読もうとしていることが、奇妙です。

一つ、思いついた可能性は、Deviseをインストールした時にお使いの環境ですでにbcryptが存在した場合です。その時は、Rails以下にに改めてインストールされることはなく、環境に存在する(正確には、インストール時に環境に存在「した」)bcrypt を使おうとするのかも知れません(存じません)。

いずれにせよ、ライブラリの読み込み優先順位が、おかしなことになっていると推測します。質問によると、一旦、bcryptを(Railsではなく、rbenvの)環境にインストールするとかなさっていたということです。その時の作業、そしてそれ以降の作業(Deviseインストールなど)において、何かゴミが残っていたのかも知れませんか。

対策ですが、私ならばこうします。端的には、Rubyを入れ直し、Railsをインストールし直します。

  1. Rubyの新しいバージョンを rbenv でインストールする(もしくは現在バージョンを一旦uninstallして、それをinstallし直す)
  2. 入れたバージョンのRubyを使う(rbenv shell 3.0.my_version)
  3. 現在のお使いのRailsアプリのgitのレポジトリから、新しいディレクトリに git cloneする
  4. そのディレクトリにて、 bundle install
    • これにより、Rails自体もbin/railsにインストールされるはず。

私の環境で、実際に試したりはしていません。
私ならば上のような指針で解決を試みる、という提案でした。ご参考になれば。

投稿2021/10/30 11:53

MasaSakano

総合スコア188

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問