###前提・実現したいこと
railsのGem"devise"を用いてログイン、ログアウト機能を実装しようとすると、emailとpasswordの二つしかできませんが、ここにユーザーネームも加えたいと思いました。
$ rails g migration add_name_to_users name
でmigrationファイルを作成して、マイグレーションファイル等々を編集して、実行しました。
###発生している問題・エラーメッセージ
3000は無事に立ち上がり、新規登録<sign_up>とログアウトはできるのですが、ログイン<sign_in>ができません。
具体的なエラーメッセージは表示されず、ターミナルに以下のようなものが出てきます。
Started GET "/" for 10.0.2.2 at 2017-01-03 09:51:29 +0000 ActiveRecord::SchemaMigration Load (1.3ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by PostsController#index as HTML Completed 401 Unauthorized in 25ms (ActiveRecord: 0.0ms) Started GET "/users/sign_in" for 10.0.2.2 at 2017-01-03 09:51:29 +0000 Processing by Devise::SessionsController#new as HTML Rendering devise/sessions/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (1.5ms) Rendered devise/sessions/new.html.erb within layouts/application (120.7ms) Completed 200 OK in 1315ms (Views: 1285.3ms | ActiveRecord: 9.0ms) Started GET "/users/sign_up" for 10.0.2.2 at 2017-01-03 09:51:32 +0000 Processing by Devise::RegistrationsController#new as HTML Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (1.3ms) Rendered devise/registrations/new.html.erb within layouts/application (34.9ms) Completed 200 OK in 315ms (Views: 312.7ms | ActiveRecord: 0.0ms) Started GET "/users/sign_in" for 10.0.2.2 at 2017-01-03 09:51:34 +0000 Processing by Devise::SessionsController#new as HTML Rendering devise/sessions/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (1.2ms) Rendered devise/sessions/new.html.erb within layouts/application (26.5ms) Completed 200 OK in 317ms (Views: 315.5ms | ActiveRecord: 0.0ms) Started POST "/users/sign_in" for 10.0.2.2 at 2017-01-03 09:51:49 +0000 Processing by Devise::SessionsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"KpY6iUscWOGeRMSQa8m5J+Zj8/X7/oqnx6O1ofpiYIu724349ctxOko0OgSp+uZGCSUfDhNbB1I973+jgH+/xQ==", "user"=>{"name"=>"dotta", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) Processing by Devise::SessionsController#new as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"KpY6iUscWOGeRMSQa8m5J+Zj8/X7/oqnx6O1ofpiYIu724349ctxOko0OgSp+uZGCSUfDhNbB1I973+jgH+/xQ==", "user"=>{"name"=>"dotta", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} Rendering devise/sessions/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (1.8ms) Rendered devise/sessions/new.html.erb within layouts/application (30.6ms) Completed 200 OK in 587ms (Views: 303.7ms | ActiveRecord: 0.0ms)
おそらく、このComplete 401 Unauthorized in...というのがエラーだと思われます。
###該当のソースコード
devise.rb
Devise.setup do |config| config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' require 'devise/orm/active_record' config.authentication_keys = [:name,:email] config.case_insensitive_keys = [:name] config.strip_whitespace_keys = [:email] config.skip_session_storage = [:http_auth] config.stretches = Rails.env.test? ? 1 : 11 config.reconfirmable = true config.expire_all_remember_me_on_sign_out = true config.password_length = 6..128 config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ config.reset_password_within = 6.hours end
routes.rb
Rails.application.routes.draw do get 'users/index' get 'users/show' devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy',:as => :destroy_user_session end devise_scope :user do namespace :api do namespace :v1 do resources :sessions, :only => [:create, :destroy] resources :registrations, :only => [:create, :destroy] end end end resources :users, only:[:show,:index] do member do get :favorites end end resources :posts do resource :favorites, only:[:create,:destroy] end root 'posts#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
application_controller.rb
class UsersController < ApplicationController def index @users=User.all end def show @user = User.find(params[:id]) end def favorites @user = User.find(params[:id]) end end
add_name_to_users.rb
class AddNameToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :name, :string , unique: true end end
sessions/new.html.erb
<h2>ログイン</h2> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name, autofocus: true %> </div> <div class="field"> <%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <% if devise_mapping.rememberable? -%> <div class="field"> <%= f.check_box :remember_me %> <%= f.label :remember_me %> </div> <% end -%> <div class="actions"> <%= f.submit "Log in" %> </div> <% end %> <%= render "devise/shared/links" %>
###試したこと
ネットの情報を頼りに、いろいろ試してみましたが、ログインだけができません。
###補足情報(言語/FW/ツール等のバージョンなど)
IDE:ruby mine
環境:vagrant
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。