初学者です。ユーザーのデータを削除をするため、
html
1 def destroy 2 @user = User.find_by(id: params[:id]) 3 @user.destroy 4 session[:user_id] = nil 5 flash[:notice] = "アカウント削除しました" 6 redirect_to("/") 7 end
という形で処理しようとしましたが、NameError in UsersController#destroy
uninitialized constant User::Message
というエラーがでます。
@user.deleteならエラーはでないのですが原因が分かりません。
rails cでやってみると、
ruby
1irb(main):001:0> @user = User.find_by(id: 12) 2 User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 12], ["LIMIT", 1]] 3=> #<User id: 12, user_name: "森", password_digest: "$2a$12$5d2VhTxwn7Z1x9tzBO2R0eEpI3ntm9fPO/.g/j09tK7...", email: "jj@jj.com", imagename: "default_user.jpg", admin: false, finishcount: 0, point: 0.0, created_at: "2020-01-05 03:26:39", updated_at: "2020-01-05 03:26:39"> 4irb(main):002:0> @user.destroy 5 (0.3ms) begin transaction 6 Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 12]] 7 Post Destroy (2.1ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 13]] 8 Accept Load (0.3ms) SELECT "accepts".* FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 12]] 9 Accept Destroy (0.3ms) DELETE FROM "accepts" WHERE "accepts"."id" = ? [["id", 6]] 10 Accept Destroy (0.5ms) DELETE FROM "accepts" WHERE "accepts"."id" = ? [["id", 7]] 11 (112.7ms) rollback transaction 12Traceback (most recent call last): 13 1: from (irb):2 14NameError (uninitialized constant User::Message)
こんな感じです。
user.rb
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 # Include default devise modules. Others available are: 5 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 6before_save { self.email = email.downcase } 7VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 8validates :email,uniqueness:true,presence:true,length: { maximum: 255 },format: { with: VALID_EMAIL_REGEX } 9 10validates :user_name,{presence: true} 11validates :user_name,length:{maximum: 40} 12 13validates :password,presence: true,if: Proc.new { |a| a.password.present? } 14validates :password,length:{maximum: 18},if: Proc.new { |a| a.password.present? } 15validates :password,length:{minimum: 6},if: Proc.new { |a| a.password.present? } 16 17 18has_many :posts, dependent: :destroy 19has_many :accepts, dependent: :destroy 20has_secure_password 21 22 23has_many :messages, dependent: :destroy 24has_many :entries, dependent: :destroy 25 26def self.search(search) 27 if search 28 User.where(['user_name LIKE?',"%#{search}%"]) 29 else 30 User.all 31 end 32end 33 34 35#devise :database_authenticatable, :registerable, 36# :recoverable, :rememberable, :validatable, :confirmable 37 38end 39
すいません、どなたかご教授をお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/05 07:41
2020/01/05 07:43 編集