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

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

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

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

Ruby on Rails

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

Q&A

解決済

3回答

1021閲覧

@current_user= User.find_by (id: session[:user_id]のエラー 新規会員登録やログインが出来ない

yoshidesu

総合スコア4

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/07/21 08:35

編集2021/07/21 15:58

お世話になっております。

問題

新規会員登録やログインをしようとし、フォームにメールアドレスや名前を入力し登録しようとしたところ 上記の
@current_user= User.find_by (id: session[:user_id]

というエラーが起きてしまい、登録できずにいます。

恐らく、@current_userに値が入れられていないためだと思うのですが、

DBからuserの情報を取得した後の、どこにそのuser情報を@current_userにセットしているかどのようにして確認するのでしょうか??

当方 初心者であまり分からず、エラーに困惑してしまいました。

どなたかヘルプ頂けたらと思います。

以下 application_controller内のコードになります

class ApplicationController < ActionController::Base before_action :set_current_user def set_current_user @current_user = User.find_by(id: session[:user_id]) end def authenticate_user if @current_user == nil flash[:notice]= "ログインが必要です" redirect_to("/login") end end def forbid_login_user if @current_user flash[:notice]= "すでにログインしています" redirect_to("/posts/index") end end end

OS 環境は mac os 10.15.6 です。

gemfile は以下です。

source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.0.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4' # Use sqlite3 as the database for Active Record gem 'sqlite3', '~> 1.4' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 5.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password gem 'bcrypt' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 4.1.0' # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md gem 'rack-mini-profiler', '~> 2.0' gem 'listen', '~> 3.3' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 3.26' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

application_controllerのコードは以下です。

class ApplicationController < ActionController::Base before_action :set_current_user def set_current_user @current_user = User.find_by(id: session[:user_id]) end def authenticate_user if @current_user == nil flash[:notice]= "ログインが必要です" redirect_to("/login") end end def forbid_login_user if @current_user flash[:notice]= "すでにログインしています" redirect_to("/posts/index") end end end

作成中のものは、Twitterのような簡単な投稿サイトデモです。

宜しくお願いします。

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

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

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

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

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

maisumakun

2021/07/21 08:36

エラーメッセージを「全文」提示していただけませんか?
yoshidesu

2021/07/21 08:51

了解いたしました。ありがとうございます。 少々お待ちください
anozon

2021/07/21 08:53

コードブロックで囲んでいただけますか?
yoshidesu

2021/07/21 08:54

以下 エラーメッセージになります。 bcryptのエラーと同時にメッセージがきているため、エラーメッセージタイトルは cannot load such file -- bcryptとなっております LoadError in HomeController#top cannot load such file -- bcrypt Extracted source (around line #5): 3 4 5 6 7 8 def set_current_user @current_user = User.find_by(id: session[:user_id]) end def authenticate_user Rails.root: /Users/yoshi/sample_tweetapp Application Trace | Framework Trace | Full Trace app/models/user.rb:3:in `<class:User>' app/models/user.rb:1:in `<main>' app/controllers/application_controller.rb:5:in `set_current_user' Request Parameters: None Toggle session dump Toggle env dump Response Headers: None x >> Context has changed to: app/models/user.rb:1:in `&lt;main&gt;' >> Context has changed to: app/controllers/application_controller.rb:5:in `set_current_user' >> Context has changed to: app/models/user.rb:3:in `&lt;class:User&gt;' >> Context has changed to: app/controllers/application_controller.rb:5:in `set_current_user' >>
yoshidesu

2021/07/21 09:01

コードブロックやってみます! LoadError in HomeController#top cannot load such file -- bcrypt ``` Extracted source (around line #5): 3 4 5 6 7 8 def set_current_user @current_user = User.find_by(id: session[:user_id]) end def authenticate_user ``` Rails.root: /Users/yoshi/sample_tweetapp Application Trace | Framework Trace | Full Trace app/models/user.rb:3:in `<class:User>' app/models/user.rb:1:in `<main>' app/controllers/application_controller.rb:5:in `set_current_user'
asm

2021/07/21 09:33

OS環境やGemfileはどうなっていますか?
winterboum

2021/07/21 15:19

def set_current_user を呼んではだめなときに呼んでないかな。 controller の最初の方載せて
yoshidesu

2021/07/21 15:59

みなさん、本当にありがとうございます,..助けられています。 os 環境についてと、gemfile,application_controllerのコード内容も本文に追加させて頂きました!
guest

回答3

0

Application Traceなので最後に実行されたapp以下のコードが表示されているだけであって
User.find_by(...)は関係ありません。
Full Traceや発生しているエラーを確認してください。


cannot load such file -- bcrypt

bcrypt gemの導入が正しく行われていないようです。

mac環境がないので再現できませんが
gem install bcryptbundle install時のエラーを基に何が問題かを把握していくことになります。

勝手に推測するのであれば
command line tools for Xcodeがインストールされている事を確認することが必要かと思います。

投稿2021/07/22 00:21

asm

総合スコア15147

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

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

yoshidesu

2021/07/22 07:12

gem install bcryptなどコマンドは試してみたのですが、以下のメッセージが表示されたので エラーは起きていないと思われるのですが、何故か cannot load such fileが続きます。。 Building native extensions. This could take a while... Successfully installed bcrypt-3.1.16 Parsing documentation for bcrypt-3.1.16 Done installing documentation for bcrypt after 0 seconds 1 gem installed
yoshidesu

2021/07/22 07:18

使用しているのはmac でvisual studio codeです!
asm

2021/07/22 16:30

ぐぐるとMacで問題が出ている例がいくつか見られますが 具体的に原因・解決策を提示しているところが見つかりませんね gem update --systemや先日リリースされたruby3.0.2の利用を試すのもよいかと思います
yoshidesu

2021/08/04 07:38

rubyのバージョンもアップデートしてみたら、違う問題ですがbcryptに関する問題が解決しました。 ご相談のって頂き、大変助かりました。
guest

0

before_action :set_current_user これが全ての場合に呼ばれています。
ので、login前の session[:user_id] がないときにも。

方法は2つ

  1. login画面を出すときとlogin 実行のときは通らないようにする
  2. session[:user_id]が無いときは設定しないで帰る

私の実力だと
ある特定のcontrollerの特定のactionだけexceptするってのは厄介だから2を採ります。
あれ?
よく見ると変だな、User.find_by(id: session[:user_id]) はエラーを起こさないと思うのだが。つまりいまのままで2になってる。

エラーメッセージを全文のせてください。

投稿2021/07/21 23:16

winterboum

総合スコア23401

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

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

0

自己解決

様々な回答いただき、application_controller内の

before_action :set_current_user

を消したら、localhost:3000が反応しホーム画面が見られるようになりました。

別の質問で設けていますが、依然としてbcryptの問題は残されたままで継続して問題解決に取り組もうと思います

投稿2021/07/22 07:03

yoshidesu

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問