前提・実現したいこと
railsでwebアプリケーションの課題に取り組んでいます。
完成間近なのですが、最後にバリデーションをかけたところ新規登録ができなくなりました。
validates :introduction, presence: true, length: {maximum: 50}
このバリデーションをプロフィールの自己紹介文にだけかけたいのですが、どうも新規登録にもかかってしまうのが原因だと考えております。
解決策のご教授をお願いいたします。
発生している問題・エラーメッセージ
/bookers2/app/models/user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :name, presence: true, uniqueness: true, length: { minimum: 2, maximum: 20} validates :introduction, presence: true, length: {maximum: 50} has_many :books, dependent: :destroy attachment :profile_image end
/bookers2/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base before_action :authenticate_user!,except: [:top, :about,] before_action :configure_permitted_parameters, if: :devise_controller? def after_sign_in_path_for(resource) user_path(resource) end def after_sign_up_path_for(resource) user_path(resource) end protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys:[:email]) end end
### /bookers2/app/views/devise/registrations/new.html.erb
<div class="container"> <div class="row"> <div class="col-sm-12 col-md-8 col-lg-5 px-5 px-sm-0 mt-3"> <h2>Sign up</h2> <%= form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user', local: true do |f| %> <%= render "devise/shared/error_messages", resource: @user %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name, autofocus: true %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="actions"> <%= f.submit "Sign up", class: "btn btn-sm btn-success" %> </div> <% end %> <%= render "devise/shared/links" %> </div> </div> </div>
試したこと
バリテーションを外せば新規登録できましたが、もちろん自己紹介文にはバリテーションはかかりませんでした。
新規登録を押した時のターミナルです。
Started POST "/users" for 119.229.201.247 at 2021-04-08 14:39:26 +0000 Cannot render console from 119.229.201.247! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Users::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"gj_bKQ4GEQOpi6om8ecVR74WPOXfEgpfUrnsfKZwHD6hHNY4zN8UBT9lY7akTAisMkNo9Sn_eSw8vF1WJsrCuQ", "user"=>{"name"=>"mkmlkm", "email"=>"adsfs@asdfaf", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} (0.1ms) begin transaction ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.5/lib/active_record/log_subscriber.rb:98 User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "adsfs@asdfaf"], ["LIMIT", 1]] ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.5/lib/active_record/log_subscriber.rb:98 User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = ? LIMIT ? [["name", "mkmlkm"], ["LIMIT", 1]] ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.5/lib/active_record/log_subscriber.rb:98 (0.0ms) rollback transaction ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.5/lib/active_record/log_subscriber.rb:98 Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_error_messages.html.erb (0.7ms) Rendered devise/shared/_links.html.erb (0.8ms) Rendered devise/registrations/new.html.erb within layouts/application (6.3ms) Completed 200 OK in 436ms (Views: 102.3ms | ActiveRecord: 1.0ms)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/09 02:18