質問編集履歴

3 member.rbを追記

tacro

tacro score 17

2018/03/30 14:29  投稿

[Rails] [Devise] サインアップ時に発生するエラー”undefined method `username'”を解決したい
#前提・実現したいこと
Rails5で開発をしています。
一通り実装が終わってから、deviseの存在を知り、ログイン周りの機能を実装し直しています。
すでにuserモデルが存在しているので、別途memberモデルをdeviseで作成し、userモデルの役割を移行しようと試みています。
以下の記事を参考にしています。
[Rails deviseによるユーザー認証 メールによる認証、emailとusernameのどちらでもログイン可能にするまで](https://qiita.com/shizuma/items/c8c2e71af8c1dcf3d1c2)
#発生している問題
サインアップを実際に行えるか試してみたところ、フォームを送信する過程で以下のエラーが発生しました。
なんとかして解決したいです、アドバイスお願い致します!
> NoMethodError in Devise::RegistrationsController#create
undefined method `username' for #<Member:0x00007fa4496a25d0> Did you mean? rename
細かなエラーメッセージは以下の通りです。
→【追記】本文の字数が足りないため、一旦省略します。
schema.rbやdbconsoleから、memberテーブルを確認してみましたが、usernameカラムはきちんと存在しています。
```Ruby
ActiveRecord::Schema.define(version: 20180330025634) do
...
 create_table "members", force: :cascade do |t|
   t.string "email", default: "", null: false
   t.string "encrypted_password", default: "", null: false
   t.string "reset_password_token"
   t.datetime "reset_password_sent_at"
   t.datetime "remember_created_at"
   t.integer "sign_in_count", default: 0, null: false
   t.datetime "current_sign_in_at"
   t.datetime "last_sign_in_at"
   t.string "current_sign_in_ip"
   t.string "last_sign_in_ip"
   t.string "confirmation_token"
   t.datetime "confirmed_at"
   t.datetime "confirmation_sent_at"
   t.string "unconfirmed_email"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.string "username"
   t.integer "user_group"
   t.string "icon_image_name"
   t.integer "gender"
   t.string "profile"
   t.index ["email"], name: "index_members_on_email", unique: true
   t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true
   t.index ["username"], name: "index_members_on_username", unique: true
 end
...
end
```
#該当のソースコード
その他、コントローラでのパラメータの設定は以下の通りです。
```Ruby
class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception
 before_action :authenticate_member!
 before_action :configure_permitted_parameters, if: :devise_controller?
 protected
 def configure_permitted_parameters
   devise_parameter_sanitizer.permit(:sign_up) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :gender, :user_group, :remember_me)
   end
   devise_parameter_sanitizer.permit(:sign_in) do |member_params|
     member_params.permit(:login, :username, :email, :password, :remember_me)
   end
   devise_parameter_sanitizer.permit(:account_update) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :current_password)
   end
 end
...
end
```
#試してみたこと
rake db:migrate:resetでデータベースのリセットを行ったりしてみましたが、解決しませんでした。
#使用している環境
Ruby on Rails 5
devise (4.4.3)
#追記
```Ruby
Rails.application.routes.draw do
 devise_for :members
 root "home#top"
# get 'signup' => "users#new"
 # get 'login' => "users#login_form"
 # post 'login' => "users#login"
 # post 'logout' => "users#logout"
 # get 'users/:id/signup_designer' => "users#new_designer"
 # patch 'users/:id/register_designer' => "users#register_designer"
 #
 # post 'users/create' => "users#create"
 # get 'users/:id' => "users#show"
 # get 'users/:id/edit' => "users#edit"
 # patch 'users/:id/update' => "users#update"
 # patch "users/:id/update_designer" => "users#update_designer"
 # get 'users/:id/likes' => "users#likes"
 # get 'users/:id/timeline' => "users#following_posts"
 get 'posts/index'=> "posts#index"
 get 'posts/new' => "posts#new"
 post 'posts/create' => "posts#create"
 post "posts/:id/comment" => "posts#comment"
 post "comments/:id/destroy" => "comments#destroy"
 get 'posts/:id' => "posts#show"
 get 'posts/:id/edit' => "posts#edit"
 patch 'posts/:id/update' => "posts#update"
 post 'posts/:id/destroy' => "posts#destroy"
 post '/likes/:post_id/create' => "likes#create"
 post '/likes/:post_id/destroy' => "likes#destroy"
 get '/' => "home#top"
 get "about" => "home#about"
 resources :users do
   member do
    get :following, :followers
   end
 end
 resources :relationships, only: [:create, :destroy]
 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
```  
 
```Ruby  
class Member  
 include Mongoid::Document  
 # Include default devise modules. Others available are:  
 # :confirmable, :lockable, :timeoutable and :omniauthable  
 attr_accessor :login  
 devise :database_authenticatable, :registerable,  
        :recoverable, :rememberable, :trackable, :validatable,  
        :confirmable, :authentication_keys => [:login]  
 
 validates :username,  
           uniqueness: { case_sensitive: :false },  
           length: { minimum: 4, maximum: 20 },  
           format: { with: /\A[a-z0-9]+\z/, message: "ユーザー名は半角英数字です"}  
 
 
 ## Database authenticatable  
 field :email,             type: String, default: ""  
 field :encrypted_password, type: String, default: ""  
 
 ## Recoverable  
 field :reset_password_token,  type: String  
 field :reset_password_sent_at, type: Time  
 
 ## Rememberable  
 field :remember_created_at, type: Time  
 
 ## Trackable  
 field :sign_in_count,     type: Integer, default: 0  
 field :current_sign_in_at, type: Time  
 field :last_sign_in_at,   type: Time  
 field :current_sign_in_ip, type: String  
 field :last_sign_in_ip,   type: String  
 
 ## Confirmable  
 field :confirmation_token,  type: String  
 field :confirmed_at,        type: Time  
 field :confirmation_sent_at, type: Time  
 field :unconfirmed_email,   type: String # Only if using reconfirmable  
 
 ## Lockable  
 # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts  
 # field :unlock_token,   type: String # Only if unlock strategy is :email or :both  
 # field :locked_at,      type: Time  
 
 def self.find_first_by_auth_conditions(warden_conditions)  
   conditions = warden_conditions.dup  
   if login = conditions.delete(:login)  
     where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first  
   else  
     where(conditions).first  
   end  
 end  
 
 def will_save_change_to_email?  
 end  
   
end  
```
  • Ruby

    12124 questions

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

  • Ruby on Rails

    11864 questions

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

  • RubyGems

    419 questions

    RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

2 routes.rbを追記しました!

tacro

tacro score 17

2018/03/30 13:38  投稿

[Rails] [Devise] サインアップ時に発生するエラー”undefined method `username'”を解決したい
#前提・実現したいこと
Rails5で開発をしています。
一通り実装が終わってから、deviseの存在を知り、ログイン周りの機能を実装し直しています。
すでにuserモデルが存在しているので、別途memberモデルをdeviseで作成し、userモデルの役割を移行しようと試みています。
以下の記事を参考にしています。
[Rails deviseによるユーザー認証 メールによる認証、emailとusernameのどちらでもログイン可能にするまで](https://qiita.com/shizuma/items/c8c2e71af8c1dcf3d1c2)
#発生している問題
サインアップを実際に行えるか試してみたところ、フォームを送信する過程で以下のエラーが発生しました。
なんとかして解決したいです、アドバイスお願い致します!
> NoMethodError in Devise::RegistrationsController#create
undefined method `username' for #<Member:0x00007fa4496a25d0> Did you mean? rename
細かなエラーメッセージは以下の通りです。
```text
mongoid (6.4.0) lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
activemodel (5.1.5) lib/active_model/validator.rb:148:in `block in validate'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `each'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `validate'
activesupport (5.1.5) lib/active_support/callbacks.rb:413:in `block in make_lambda'
activesupport (5.1.5) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `catch'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `block in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:198:in `block in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:130:in `run_callbacks'
mongoid (6.4.0) lib/mongoid/interceptable.rb:132:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.5) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
mongoid (6.4.0) lib/mongoid/interceptable.rb:132:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations.rb:335:in `valid?'
mongoid (6.4.0) lib/mongoid/validatable.rb:97:in `valid?'
activemodel (5.1.5) lib/active_model/validations.rb:372:in `invalid?'
mongoid (6.4.0) lib/mongoid/persistable/creatable.rb:117:in `prepare_insert'
mongoid (6.4.0) lib/mongoid/persistable/creatable.rb:23:in `insert'
mongoid (6.4.0) lib/mongoid/persistable/savable.rb:23:in `save'
devise (4.4.3) app/controllers/devise/registrations_controller.rb:19:in `create'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.1.5) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) 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.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.2) lib/puma/configuration.rb:225:in `call'
puma (3.11.2) lib/puma/server.rb:624:in `handle_request'
puma (3.11.2) lib/puma/server.rb:438:in `process_client'
puma (3.11.2) lib/puma/server.rb:302:in `block in run'
puma (3.11.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
```
→【追記】本文の字数が足りないため、一旦省略します。
schema.rbやdbconsoleから、memberテーブルを確認してみましたが、usernameカラムはきちんと存在しています。
```Ruby
ActiveRecord::Schema.define(version: 20180330025634) do
...
 create_table "members", force: :cascade do |t|
   t.string "email", default: "", null: false
   t.string "encrypted_password", default: "", null: false
   t.string "reset_password_token"
   t.datetime "reset_password_sent_at"
   t.datetime "remember_created_at"
   t.integer "sign_in_count", default: 0, null: false
   t.datetime "current_sign_in_at"
   t.datetime "last_sign_in_at"
   t.string "current_sign_in_ip"
   t.string "last_sign_in_ip"
   t.string "confirmation_token"
   t.datetime "confirmed_at"
   t.datetime "confirmation_sent_at"
   t.string "unconfirmed_email"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.string "username"
   t.integer "user_group"
   t.string "icon_image_name"
   t.integer "gender"
   t.string "profile"
   t.index ["email"], name: "index_members_on_email", unique: true
   t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true
   t.index ["username"], name: "index_members_on_username", unique: true
 end
...
end
```
#該当のソースコード
その他、コントローラでのパラメータの設定は以下の通りです。
```Ruby
class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception
 before_action :authenticate_member!
 before_action :configure_permitted_parameters, if: :devise_controller?
 protected
 def configure_permitted_parameters
   devise_parameter_sanitizer.permit(:sign_up) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :gender, :user_group, :remember_me)
   end
   devise_parameter_sanitizer.permit(:sign_in) do |member_params|
     member_params.permit(:login, :username, :email, :password, :remember_me)
   end
   devise_parameter_sanitizer.permit(:account_update) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :current_password)
   end
 end
...
end
```
#試してみたこと
rake db:migrate:resetでデータベースのリセットを行ったりしてみましたが、解決しませんでした。
#使用している環境
Ruby on Rails 5
devise (4.4.3)
devise (4.4.3)
#追記
```Ruby
Rails.application.routes.draw do
 devise_for :members
 root "home#top"
# get 'signup' => "users#new"
 # get 'login' => "users#login_form"
 # post 'login' => "users#login"
 # post 'logout' => "users#logout"
 # get 'users/:id/signup_designer' => "users#new_designer"
 # patch 'users/:id/register_designer' => "users#register_designer"
 #
 # post 'users/create' => "users#create"
 # get 'users/:id' => "users#show"
 # get 'users/:id/edit' => "users#edit"
 # patch 'users/:id/update' => "users#update"
 # patch "users/:id/update_designer" => "users#update_designer"
 # get 'users/:id/likes' => "users#likes"
 # get 'users/:id/timeline' => "users#following_posts"
 get 'posts/index'=> "posts#index"
 get 'posts/new' => "posts#new"
 post 'posts/create' => "posts#create"
 post "posts/:id/comment" => "posts#comment"
 post "comments/:id/destroy" => "comments#destroy"
 get 'posts/:id' => "posts#show"
 get 'posts/:id/edit' => "posts#edit"
 patch 'posts/:id/update' => "posts#update"
 post 'posts/:id/destroy' => "posts#destroy"
 post '/likes/:post_id/create' => "likes#create"
 post '/likes/:post_id/destroy' => "likes#destroy"
 get '/' => "home#top"
 get "about" => "home#about"
 resources :users do
   member do
    get :following, :followers
   end
 end
 resources :relationships, only: [:create, :destroy]
 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
```
  • Ruby

    12124 questions

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

  • Ruby on Rails

    11864 questions

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

  • RubyGems

    419 questions

    RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

1 エラーメッセージの記法を引用からコードに変更

tacro

tacro score 17

2018/03/30 13:09  投稿

[Rails] [Devise] サインアップ時に発生するエラー”undefined method `username'”を解決したい
#前提・実現したいこと
Rails5で開発をしています。
一通り実装が終わってから、deviseの存在を知り、ログイン周りの機能を実装し直しています。
すでにuserモデルが存在しているので、別途memberモデルをdeviseで作成し、userモデルの役割を移行しようと試みています。
以下の記事を参考にしています。
[Rails deviseによるユーザー認証 メールによる認証、emailとusernameのどちらでもログイン可能にするまで](https://qiita.com/shizuma/items/c8c2e71af8c1dcf3d1c2)
#発生している問題
サインアップを実際に行えるか試してみたところ、フォームを送信する過程で以下のエラーが発生しました。
なんとかして解決したいです、アドバイスお願い致します!
> NoMethodError in Devise::RegistrationsController#create
undefined method `username' for #<Member:0x00007fa4496a25d0> Did you mean? rename
細かなエラーメッセージは以下の通りです。
> mongoid (6.4.0) lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
```text
mongoid (6.4.0) lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
activemodel (5.1.5) lib/active_model/validator.rb:148:in `block in validate'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `each'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `validate'
activesupport (5.1.5) lib/active_support/callbacks.rb:413:in `block in make_lambda'
activesupport (5.1.5) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `catch'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `block in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:198:in `block in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:130:in `run_callbacks'
mongoid (6.4.0) lib/mongoid/interceptable.rb:132:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.5) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
mongoid (6.4.0) lib/mongoid/interceptable.rb:132:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations.rb:335:in `valid?'
mongoid (6.4.0) lib/mongoid/validatable.rb:97:in `valid?'
activemodel (5.1.5) lib/active_model/validations.rb:372:in `invalid?'
mongoid (6.4.0) lib/mongoid/persistable/creatable.rb:117:in `prepare_insert'
mongoid (6.4.0) lib/mongoid/persistable/creatable.rb:23:in `insert'
mongoid (6.4.0) lib/mongoid/persistable/savable.rb:23:in `save'
devise (4.4.3) app/controllers/devise/registrations_controller.rb:19:in `create'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.1.5) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) 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.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.2) lib/puma/configuration.rb:225:in `call'
puma (3.11.2) lib/puma/server.rb:624:in `handle_request'
puma (3.11.2) lib/puma/server.rb:438:in `process_client'
puma (3.11.2) lib/puma/server.rb:302:in `block in run'
puma (3.11.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
```  
schema.rbやdbconsoleから、memberテーブルを確認してみましたが、usernameカラムはきちんと存在しています。
```Ruby
ActiveRecord::Schema.define(version: 20180330025634) do
...
 create_table "members", force: :cascade do |t|
   t.string "email", default: "", null: false
   t.string "encrypted_password", default: "", null: false
   t.string "reset_password_token"
   t.datetime "reset_password_sent_at"
   t.datetime "remember_created_at"
   t.integer "sign_in_count", default: 0, null: false
   t.datetime "current_sign_in_at"
   t.datetime "last_sign_in_at"
   t.string "current_sign_in_ip"
   t.string "last_sign_in_ip"
   t.string "confirmation_token"
   t.datetime "confirmed_at"
   t.datetime "confirmation_sent_at"
   t.string "unconfirmed_email"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.string "username"
   t.integer "user_group"
   t.string "icon_image_name"
   t.integer "gender"
   t.string "profile"
   t.index ["email"], name: "index_members_on_email", unique: true
   t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true
   t.index ["username"], name: "index_members_on_username", unique: true
 end
...
end
```
#該当のソースコード
その他、コントローラでのパラメータの設定は以下の通りです。
```Ruby
class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception
 before_action :authenticate_member!
 before_action :configure_permitted_parameters, if: :devise_controller?
 protected
 def configure_permitted_parameters
   devise_parameter_sanitizer.permit(:sign_up) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :gender, :user_group, :remember_me)
   end
   devise_parameter_sanitizer.permit(:sign_in) do |member_params|
     member_params.permit(:login, :username, :email, :password, :remember_me)
   end
   devise_parameter_sanitizer.permit(:account_update) do |member_params|
     member_params.permit(:username, :email, :password, :password_confirmation, :current_password)
   end
 end
...
end
```
#試してみたこと
rake db:migrate:resetでデータベースのリセットを行ったりしてみましたが、解決しませんでした。
#使用している環境
Ruby on Rails 5
devise (4.4.3)
  • Ruby

    12124 questions

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

  • Ruby on Rails

    11864 questions

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

  • RubyGems

    419 questions

    RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る