###ターミナル An error occurred while loading ./spec/models/user_spec.rb. Failure/Error: validates :nickname NoMethodError: undefined method `validates' for main:Object # ./app/models/user.rb:11:in `block in <main>' # ./app/models/user.rb:10:in `<main>' # ./config/routes.rb:2:in `block in <main>' # ./config/routes.rb:1:in `<main>' # ./config/environment.rb:5:in `<top (required)>' # ./spec/rails_helper.rb:4:in `require' # ./spec/rails_helper.rb:4:in `<top (required)>' # ./spec/models/user_spec.rb:1:in `require' # ./spec/models/user_spec.rb:1:in `<top (required)>' No examples found. Finished in 0.00006 seconds (files took 2.49 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples ###Userモデル class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :items has_many :purchases end with_options presence: true do validates :nickname validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :birth_day validates :password, length: { minimum: 6 }, format: { with: /\A[a-z0-9]+\z/i, message: "英数字文字6以上"} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } end
二日ほど悩んでいます。ご教授よろしくお願いします。 ## エラー文 SyntaxError: /Users/yudaitaguchi/projects/furima-30611/app/models/user.rb:12: syntax error, unexpected tSYMBEG, expecting do or '{' or '(' validates :family_name, format: { with: ... ^ /Users/yudaitaguchi/projects/furima-30611/app/models/user.rb:17: syntax error, unexpected tSYMBEG, expecting do or '{' or '(' validates :password, length: { minimum: ... ### ターミナル yudaitaguchi@taguchidainoMBP furima-30611 % bundle exec rspec spec/models/user_spec.rb An error occurred while loading ./spec/models/user_spec.rb. Failure/Error: require File.expand_path('../config/environment', __dir__) SyntaxError: /Users/yudaitaguchi/projects/furima-30611/app/models/user.rb:12: syntax error, unexpected tSYMBEG, expecting do or '{' or '(' validates :family_name, format: { with: ... ^ /Users/yudaitaguchi/projects/furima-30611/app/models/user.rb:17: syntax error, unexpected tSYMBEG, expecting do or '{' or '(' validates :password, length: { minimum: ... ^ # ./config/routes.rb:2:in `block in <main>' # ./config/routes.rb:1:in `<main>' # ./config/environment.rb:5:in `<top (required)>' # ./spec/rails_helper.rb:4:in `require' # ./spec/rails_helper.rb:4:in `<top (required)>' # ./spec/models/user_spec.rb:1:in `require' # ./spec/models/user_spec.rb:1:in `<top (required)>' No examples found. Finished in 0.00006 seconds (files took 0.93201 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples ### Userモデル class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :items has_many :purchases end with_options presence: true do validates :nickname, validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :birth_day, validates :password, length: { minimum: 6 }, format: { with: /\A[a-z0-9]+\z/i, message: "英数字文字6以上"} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } end ### Factorybot FactoryBot.define do factory :user do nickname {"abe"} email {"kkk@gmail.com"} password {"00000a"} password_confirmation {"00000a"} family_name {"ぜんかく"} first_name {"ぜんかく"} family_name_kana {"ゼンカクカナ"} first_name_kana {"ゼンカクカナ"} birth_day {"2020-01-01"} end end ### 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 has_many :items has_many :purchases end with_options presence: true do validates :nickname, validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]+\z/, message: '全角文字を使用してください' } validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "全角カタカナを使用してください" } validates :birth_day, validates :password, length: { minimum: 6 }, format: { with: /\A[a-z0-9]+\z/i, message: "英数字文字6以上"} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } end
user.rbのcodeはいま画面に見える通りに記述してますか? 1行に2回 validatesがみえますが。
<code>をつかって、記述通りに見えるように質問を修正してください。
ご回答ありがとうございます!
今あるカラムは必要なものなので削除できません。カンマを外してもpresence: trueは
nicknameとbirth_dayにかかるのでしょうか?
カラムを削除せよとは言ってません。validatesを、です。
validationが必要なら、完結してください
ああ、with_options presence: true みおとしてました。
ならカンマ外せば完結ですね。
undefined method `validates' for main:Object
は
with_options presence: true のすぐ上に余分な end がはいったため
回答1件
あなたの回答
tips
プレビュー