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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1861閲覧

モデルの単体テスコードでエラーが出る

Arale...3

総合スコア9

Ruby

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

Ruby on Rails

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

0グッド

1クリップ

投稿2020/10/23 13:55

編集2020/10/25 06:09
###ターミナル 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

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

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

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

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

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

winterboum

2020/10/24 03:48

user.rbのcodeはいま画面に見える通りに記述してますか? 1行に2回 validatesがみえますが。 <code>をつかって、記述通りに見えるように質問を修正してください。
Arale...3

2020/10/25 05:31

ご回答ありがとうございます! 今あるカラムは必要なものなので削除できません。カンマを外してもpresence: trueは nicknameとbirth_dayにかかるのでしょうか?
winterboum

2020/10/25 06:30

カラムを削除せよとは言ってません。validatesを、です。 validationが必要なら、完結してください
winterboum

2020/10/25 06:32

ああ、with_options presence: true みおとしてました。 ならカンマ外せば完結ですね。
winterboum

2020/10/25 06:32

undefined method `validates' for main:Object は with_options presence: true のすぐ上に余分な end がはいったため
guest

回答1

0

ベストアンサー

validates :nickname,  と validates :birth_day,  がカンマで終わっているため次のvalidatesとつながってます。この2行を完結させるか、削除してください

投稿2020/10/25 05:23

編集2020/10/25 05:27
winterboum

総合スコア23324

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

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

Arale...3

2020/10/25 06:36

解決できました!本当に悩んでいたのでとても為になりました!ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問