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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

2981閲覧

sorceryでログインが出来ません。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2018/01/24 08:51

編集2018/01/29 09:35

前提・実現したいこと

railsでsorceryを使ってログイン機能を作成しようとしているのですが、
思うように作れず困っております。
特にエラーも発生していないように見えるのですが、
画面遷移しないでいます。

私が初心者でrails開発を任されている上、社内にも詳しい者がいない状況です。
どうか皆様のお力を拝借できればと思います。

該当のソースコード

Started GET "/login?utf8=%E2%9C%93&authenticity_token=mst4jXA4YTO%2F2Z60HMf19jl3ZZgH0tYhfrMMb73sGKvpXiote7%2FFThLdH2gipdTjxg%2Ff2JquEh5XSPn88XRAIg%3D%3D&email=test%40test.com&password=[FILTERED]&commit=%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3" for 10.0.2.2 at 2018-01-29 09:33:07 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by SessionsController#new as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"mst4jXA4YTO/2Z60HMf19jl3ZZgH0tYhfrMMb73sGKvpXiote7/FThLdH2gipdTjxg/f2JquEh5XSPn88XRAIg==", "email"=>"test@test.com", "password"=>"[FILTERED]", "commit"=>"ログイン"} Rendering sessions/new.html.erb within layouts/application Rendered sessions/new.html.erb within layouts/application (1.3ms) Completed 200 OK in 268ms (Views: 238.2ms | ActiveRecord: 0.0ms)
Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root :to => 'sessions#new' resources :sessions resources :users, except: [:edit, :index] resources :clients, except: [:show, :new, :edit] resources :schedules, except: [:show, :new, :edit] get 'login' => 'sessions#new', :as => :login post 'logout' => 'sessions#destroy', :as => :logout get 'tops/index' => 'tops#index' get 'clients/index' => 'clients#index' post 'clients/index' => 'clients#index' get 'schedules/index' => 'schedules#index' post 'schedules/index' => 'schedules#index' end

sorcery.rb

1Rails.application.config.sorcery.submodules = [:remember_me] 2Rails.application.config.sorcery.configure do |config| 3 config.user_config do |user| 4 user.username_attribute_names = :email 5 end 6 config.user_class = 'User' 7end

controller

1class SessionsController < ApplicationController 2 3 def new 4 end 5 6 def create 7 user = login(params[:email], params[:password]) 8 if user 9 redirect_back_or_to tops_index_path, notice: "Login successful" 10 else 11 flash.now[:alert] = "Failed to login" 12 render action: 'new' 13 end 14 end 15 16 def destroy 17 logout 18 redirect_to root_path, notice: "Logged out!" 19 end 20 21end 22 23

model

1class User < ApplicationRecord 2 authenticates_with_sorcery! 3 4 5 validates :email, uniqueness: true 6 validates :password, length: { minimum: 6 }, if: -> { new_record? || changes["password"] } 7 validates :password, confirmation: true, if: -> { new_record? || changes["password"] } 8 validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] } 9end 10

view

1class User < ApplicationRecord 2 authenticates_with_sorcery! 3 4 5 validates :email, uniqueness: true 6 validates :password, length: { minimum: 6 }, if: -> { new_record? || changes["password"] } 7 validates :password, confirmation: true, if: -> { new_record? || changes["password"] } 8 validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] } 9end 10

試したこと

ルーティングなどいろいろ試してみましたが、
全く前に進めず困り果てております。

補足情報

ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
Rails 5.1.4
centos
MariaDB

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

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

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

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

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

guest

回答1

0

ベストアンサー

コードを拝見すると,crypted_password カラムの役割を誤解されているようです。

これは暗号化されたパスワードが入るところです。

試しに rails console で

rb

1user = User.new 2user.password = user.password_confirmation = "hogehoge" 3user.email = "foo@example.com" 4user.save

とでもやってみてください。
save に成功したら,データベースの中身をみて,users テーブルの crypted_password カラムの値がどんなだか見てみてください。
"hogehoge" とはまるで違う,長いワケの分からない文字列になっていることが見て取れるでしょう。
一方,users テーブルには passwordpassword_confirmation というカラムは無い,ということも分かります。

password を暗号化した文字列を作成して crypted_passoword カラムに収めるのは sorcery が自動的にやってくれます。
なので,例えば validates :crypted_password のように,コード中にあらわに crypted_password を書いたりしません。

……というあたりに注意して,もう一度参考サイトと照らし合わせて修正してみてください。

それから,

画面遷移しないでいます。

ではよく分かりませんので,どんなパスでアクセスしてどんな操作をしたらどう画面遷移するはずだったのに実際にはこうなった,ということを追記していただけますか。

投稿2018/01/25 03:12

scivola

総合スコア2108

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

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

退会済みユーザー

退会済みユーザー

2018/01/30 03:43

返信ありがとうございました。modelは勘違いしておりました。訂正しました。ただやはり、どうしてもログインボタンを押した際にページが変わることなく、エラーも出ず、そのままのページにレンダリングするままです。いったん、この機能用にアプリ作ってみたのですが結果は同じです。再度簡潔にまとめて質問しなおします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問