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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

Q&A

解決済

1回答

5172閲覧

【rails】【devise】新規登録画面にアクセスした段階で入力エラーの内容が表示される

gogoackman3

総合スコア109

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

0グッド

1クリップ

投稿2016/10/21 12:25

編集2016/10/21 12:29

表題の通り、新規登録画面への初回アクセス時点で既にフォームのエラー内容が表示されます。

当然、初回アクセス時点ではフォームに何も入力されていないので、それでバリデーションがかかると、「空です」「何文字以上にしてください」などすべてのバリデーションに引っかかります。

ログイン画面だと初回アクセス時点ではフォームのエラーは表示されないのですが、何故か分かりかねています・・・。

(もし分かれば追加で、)ログイン画面では、フォームでのエラーが「メールアドレスまたはパスワードが違います。」となるのですが、「メールアドレス/ユーザーIDまたはパスワードが違います。」と変更するにはどうすれば良いのでしょうか??

※ruby 2.3.1p11/Rails 5.0.0.1

▼新規登録画面

ruby

1 2section.signup_area 3 4 = form_for(resource, as: resource_name, url: user_registration_path) do |f| 5 .form_item 6 label 7 = image_tag('icon_mail.png') 8 | メールアドレス 9 = f.email_field :email, autofocus: true, class: 'register_info', placeholder: '有効なメールアドレスを入力', 'aria-required': 'true', size: 30 10 = error_message(resource, :email) 11 12 .form_item 13 label 14 = image_tag('icon_key.png') 15 | パスワード 16 = f.password_field :password, autocomplete: "off", class: 'register_info', placeholder: '半角英数字混合で6文字以上', 'aria-required': 'true', size: 30 17 = error_message(resource, :password) 18 19 p.terms_link 20 | 登録すると 21 = link_to '利用規約', terms_of_use_path 22 | 及び 23 = link_to 'プライバシーポリシー', privacy_policy_path 24 |に同意したことになります 25 26 div 27 = f.submit 'この内容で新規登録'

▼ヘルパーerror_message

ruby

1module ApplicationHelper 2 3 def error_message(resource, attr_name) 4 if resource.errors[attr_name].present? 5 %Q(<p class="error">#{ resource.errors.full_messages_for(attr_name).join('<br>')}</p>) 6 end.to_s.html_safe 7 end 8end

▼新規登録のController

ruby

1class Users::RegistrationsController < Devise::RegistrationsController 2# before_action :configure_sign_up_params, only: [:create] 3# before_action :configure_account_update_params, only: [:update] 4 5 prepend_before_action :authenticate_scope!, only: [:edit_detail, :update_detail] 6 7 8 # GET /resource/sign_up 9 def new 10 super 11 end 12 13 def edit_detail 14 15 end 16 17 def update_detail 18 resource_updated = resource.update(detail_params) 19 if resource_updated 20 @modal = {name: 'update-detail-modal'} 21 end 22 render :edit_detail 23 end 24 25 protected 26 27 # If you have extra params to permit, append them to the sanitizer. 28 # def configure_sign_up_params 29 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 30 # end 31 32 # If you have extra params to permit, append them to the sanitizer. 33 # def configure_account_update_params 34 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 35 # end 36 37 def detail_params 38 params.require(:user).permit(:icon, :handle_name, :profile) 39 end 40 41end

▼ログイン画面

ruby

1section.login_area 2 - if flash[:alert].present? 3 p.error = flash[:alert] 4 5 = form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| 6 .form_item 7 label 8 = image_tag 'icon_mail.png' 9 | メールアドレス/ユーザー名 10 = f.text_field :login, autofocus: true, class: 'register_info', placeholder: 'メールアドレスまたはユーザー名を入力', 'aria-required': true, size: 30 11 = error_message(resource, :login) 12 13 .form_item 14 label 15 = image_tag 'icon_key.png' 16 | パスワード 17 = f.password_field :password, autocomplete: "off", class: 'register_info', placeholder: '半角英数字混合で6文字以上', 'aria-required': true, size: 30 18 = error_message(resource, :password) 19 div 20 = f.submit 'ログイン' 21 22 p.fogot_pass 23 = link_to 'パスワードをお忘れの方はこちら', new_password_path(resource_name)

▼ログインのController

Ruby

1class Users::SessionsController < Devise::SessionsController 2# before_action :configure_sign_in_params, only: [:create] 3 4 # GET /resource/sign_in 5 def new 6 super 7 end 8 9end

▼user.rb

ruby

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :trackable, :validatable, 6 :omniauthable, omniauth_providers: [:twitter],authentication_keys: [:login] 7 8 # ログイン時のIDをメアドとhandle_nameの両対応 9 attr_accessor :login 10 11 validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }, length: { maximum: 255 }, on: :create 12 validates :password, format: { with: /\A[0-9a-z]+\z/i }, length: { minimum: 6 }, on: :create 13 validates :handle_name, length: { minimum:3, maximum:15 }, format: { with: /\A[a-zA-Z0-9_]+\z/, message: "ユーザー名は半角英数字またはアンダーバー(_)のみ利用可能です。"} 14 15 with_options if: :persisted? do |user| 16 user.validates :handle_name, presence: true 17 user.validates :handle_name, length: { maximum: 15 } 18 user.validates :profile, length: { maximum: 100 } 19 user.validates :email, confirmation: true 20 end 21 22 # mount_uploader :icon, IconUploader 23 24 def self.from_omniouth(auth) 25 where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user| 26 user.provider = auth["provider"] 27 user.uid = auth["uid"] 28 user.username = auth["info"]["nickname"] 29 end 30 end 31 32 def self.new_with_session(params,session) 33 if session["devise.user_attributes"] 34 new(session["devise.user_attributes"]) do |user| 35 user.attributes = params 36 user.valid? 37 end 38 else 39 super 40 end 41 end 42 43 def self.find_first_by_auth_conditions(warden_conditions) 44 conditions = warden_conditions.dup 45 if login = conditons.delete(:login) 46 where(conditions).where(["handle_name = :value OR lower(email) = lower(:value)", { :value => login }]).first 47 else 48 where(conditions).first 49 end 50 end 51end

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

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

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

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

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

guest

回答1

0

ベストアンサー

ruby

1# user.rb 2 3session["devise.user_attributes"]

に何か値が入ってしまっているのでは無いでしょうか?

その場合、新規登録のコントローラーの

ruby

1 def new 2 super 3 end

super

ruby

1# user.rb 2 def self.new_with_session(params,session) 3 if session["devise.user_attributes"] 4 new(session["devise.user_attributes"]) do |user| 5 user.attributes = params 6 user.valid? 7 end 8 else 9 super 10 end 11 end

が呼ばれるため、
user.valid?が走り、errorがセットされた状態でformに渡ってしまいます。

投稿2016/10/21 15:51

cameluby

総合スコア891

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

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

gogoackman3

2016/10/21 16:09

ありがとうございます! self.new_with_sessionの部分をコメントアウトすると確かに初回アクセス時にエラーメッセージが表示されなくなりました! やりたいことはTwitterログインとメアドログインを両方実装したく、そのため、前述のメソッドを定義していました。 また、再度調べて考えてみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問