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

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

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

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

Ruby

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

RubyGems

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

Ruby on Rails

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

Q&A

解決済

1回答

2054閲覧

[Rails] [Devise] サインアップ時に発生するエラー”undefined method `username'”を解決したい

tacro

総合スコア23

Ruby on Rails 5

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

Ruby

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

RubyGems

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

Ruby on Rails

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

0グッド

1クリップ

投稿2018/03/30 04:08

編集2018/03/30 05:29

#前提・実現したいこと
Rails5で開発をしています。
一通り実装が終わってから、deviseの存在を知り、ログイン周りの機能を実装し直しています。
すでにuserモデルが存在しているので、別途memberモデルをdeviseで作成し、userモデルの役割を移行しようと試みています。
以下の記事を参考にしています。
Rails deviseによるユーザー認証 メールによる認証、emailとusernameのどちらでもログイン可能にするまで

#発生している問題
サインアップを実際に行えるか試してみたところ、フォームを送信する過程で以下のエラーが発生しました。
なんとかして解決したいです、アドバイスお願い致します!

NoMethodError in Devise::RegistrationsController#create

undefined method `username' for #Member:0x00007fa4496a25d0 Did you mean? rename

細かなエラーメッセージは以下の通りです。
→【追記】本文の字数が足りないため、一旦省略します。

schema.rbやdbconsoleから、memberテーブルを確認してみましたが、usernameカラムはきちんと存在しています。

Ruby

1ActiveRecord::Schema.define(version: 20180330025634) do 2... 3 create_table "members", force: :cascade do |t| 4 t.string "email", default: "", null: false 5 t.string "encrypted_password", default: "", null: false 6 t.string "reset_password_token" 7 t.datetime "reset_password_sent_at" 8 t.datetime "remember_created_at" 9 t.integer "sign_in_count", default: 0, null: false 10 t.datetime "current_sign_in_at" 11 t.datetime "last_sign_in_at" 12 t.string "current_sign_in_ip" 13 t.string "last_sign_in_ip" 14 t.string "confirmation_token" 15 t.datetime "confirmed_at" 16 t.datetime "confirmation_sent_at" 17 t.string "unconfirmed_email" 18 t.datetime "created_at", null: false 19 t.datetime "updated_at", null: false 20 t.string "username" 21 t.integer "user_group" 22 t.string "icon_image_name" 23 t.integer "gender" 24 t.string "profile" 25 t.index ["email"], name: "index_members_on_email", unique: true 26 t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true 27 t.index ["username"], name: "index_members_on_username", unique: true 28 end 29... 30end

#該当のソースコード
その他、コントローラでのパラメータの設定は以下の通りです。

Ruby

1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :authenticate_member! 4 5 before_action :configure_permitted_parameters, if: :devise_controller? 6 7 protected 8 9 def configure_permitted_parameters 10 devise_parameter_sanitizer.permit(:sign_up) do |member_params| 11 member_params.permit(:username, :email, :password, :password_confirmation, :gender, :user_group, :remember_me) 12 end 13 devise_parameter_sanitizer.permit(:sign_in) do |member_params| 14 member_params.permit(:login, :username, :email, :password, :remember_me) 15 end 16 devise_parameter_sanitizer.permit(:account_update) do |member_params| 17 member_params.permit(:username, :email, :password, :password_confirmation, :current_password) 18 end 19 end 20... 21end

#試してみたこと
rake db:migrate:resetでデータベースのリセットを行ったりしてみましたが、解決しませんでした。

#使用している環境
Ruby on Rails 5
devise (4.4.3)

#追記

Ruby

1Rails.application.routes.draw do 2 devise_for :members 3 root "home#top" 4 5 # get 'signup' => "users#new" 6 # get 'login' => "users#login_form" 7 # post 'login' => "users#login" 8 # post 'logout' => "users#logout" 9 # get 'users/:id/signup_designer' => "users#new_designer" 10 # patch 'users/:id/register_designer' => "users#register_designer" 11 # 12 # post 'users/create' => "users#create" 13 # get 'users/:id' => "users#show" 14 # get 'users/:id/edit' => "users#edit" 15 # patch 'users/:id/update' => "users#update" 16 # patch "users/:id/update_designer" => "users#update_designer" 17 # get 'users/:id/likes' => "users#likes" 18 # get 'users/:id/timeline' => "users#following_posts" 19 20 get 'posts/index'=> "posts#index" 21 get 'posts/new' => "posts#new" 22 post 'posts/create' => "posts#create" 23 post "posts/:id/comment" => "posts#comment" 24 25 post "comments/:id/destroy" => "comments#destroy" 26 27 get 'posts/:id' => "posts#show" 28 get 'posts/:id/edit' => "posts#edit" 29 patch 'posts/:id/update' => "posts#update" 30 post 'posts/:id/destroy' => "posts#destroy" 31 32 post '/likes/:post_id/create' => "likes#create" 33 post '/likes/:post_id/destroy' => "likes#destroy" 34 35 get '/' => "home#top" 36 get "about" => "home#about" 37 38 resources :users do 39 member do 40 get :following, :followers 41 end 42 end 43 resources :relationships, only: [:create, :destroy] 44 45 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 46end 47

Ruby

1class Member 2 include Mongoid::Document 3 # Include default devise modules. Others available are: 4 # :confirmable, :lockable, :timeoutable and :omniauthable 5 attr_accessor :login 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :trackable, :validatable, 8 :confirmable, :authentication_keys => [:login] 9 10 validates :username, 11 uniqueness: { case_sensitive: :false }, 12 length: { minimum: 4, maximum: 20 }, 13 format: { with: /\A[a-z0-9]+\z/, message: "ユーザー名は半角英数字です"} 14 15 16 ## Database authenticatable 17 field :email, type: String, default: "" 18 field :encrypted_password, type: String, default: "" 19 20 ## Recoverable 21 field :reset_password_token, type: String 22 field :reset_password_sent_at, type: Time 23 24 ## Rememberable 25 field :remember_created_at, type: Time 26 27 ## Trackable 28 field :sign_in_count, type: Integer, default: 0 29 field :current_sign_in_at, type: Time 30 field :last_sign_in_at, type: Time 31 field :current_sign_in_ip, type: String 32 field :last_sign_in_ip, type: String 33 34 ## Confirmable 35 field :confirmation_token, type: String 36 field :confirmed_at, type: Time 37 field :confirmation_sent_at, type: Time 38 field :unconfirmed_email, type: String # Only if using reconfirmable 39 40 ## Lockable 41 # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts 42 # field :unlock_token, type: String # Only if unlock strategy is :email or :both 43 # field :locked_at, type: Time 44 45 def self.find_first_by_auth_conditions(warden_conditions) 46 conditions = warden_conditions.dup 47 if login = conditions.delete(:login) 48 where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first 49 else 50 where(conditions).first 51 end 52 end 53 54 def will_save_change_to_email? 55 end 56 57end

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

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

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

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

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

KaiShoya

2018/03/30 04:31

routes.rbの中を見せて下さい
tacro

2018/03/30 04:39

追記致しました!よろしくお願いします。
Hungmi

2018/03/30 05:27

member.rbの中を見せて下さい
tacro

2018/03/30 05:29

追記致しました!よろしくお願いします。
guest

回答1

0

自己解決

自己解決しました!
memberモデルが Mongoidを使用していたのが悪影響していたみたいです。
Gemから Mongoidを一旦アンインストールして、ApplicationRecordで作成しなしたらうまくいきました!
反応いただいた方、ありがとうございました!

投稿2018/03/31 00:26

tacro

総合スコア23

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問