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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1354閲覧

rails チュートリアルのエラー

pome00000

総合スコア5

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/05/22 14:21

編集2020/05/22 14:58

<困っていること>
railsチュートリアル8章の後半で今まで通ってきたテストの全てがエラーになりました。
デバックしようとしましたが
エラーが多すぎてどこから手をつけたらいいかわからず
とても困ってしまった為質問させていただきます。

<エラー>
E

Error:
SessionsControllerTest#test_should_get_new:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
SessionsControllerTest#test_should_get_new:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/sessions_controller_test.rb:4

E

Error:
StaticPagesControllerTest#test_should_get_help:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
StaticPagesControllerTest#test_should_get_help:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/static_pages_controller_test.rb:11

E

Error:
StaticPagesControllerTest#test_should_get_home:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
StaticPagesControllerTest#test_should_get_home:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/static_pages_controller_test.rb:5

E

Error:
StaticPagesControllerTest#test_should_get_about:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
StaticPagesControllerTest#test_should_get_about:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/static_pages_controller_test.rb:17

E

Error:
StaticPagesControllerTest#test_should_get_contact:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
StaticPagesControllerTest#test_should_get_contact:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/static_pages_controller_test.rb:23

E

Error:
UsersLoginTest#test_login_with_invalid_information:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
UsersLoginTest#test_login_with_invalid_information:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/integration/users_login_test.rb:9

E

Error:
UsersLoginTest#test_login_with_valid_information:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
UsersLoginTest#test_login_with_valid_information:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/integration/users_login_test.rb:19

E

Error:
UsersSignupTest#test_valid_signup_information:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
UsersSignupTest#test_valid_signup_information:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/integration/users_signup_test.rb:17

E

Error:
UsersSignupTest#test_invalid_signup_information:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
UsersSignupTest#test_invalid_signup_information:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/integration/users_signup_test.rb:5

E

Error:
UsersControllerTest#test_should_get_new:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
UsersControllerTest#test_should_get_new:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/controllers/users_controller_test.rb:5

E

Error:
UserTest#test_password_should_be_present_(nonblank):
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:59

E

Error:
UserTest#test_email_should_not_be_too_long:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:30

E

Error:
UserTest#test_password_should_have_a_minimum_length:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:64

E

Error:
UserTest#test_email_should_be_present:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:20

E

Error:
UserTest#test_name_should_not_be_too_long:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:25

E

Error:
UserTest#test_email_validation_should_accept_valid_addresses:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:35

E

Error:
UserTest#test_name_should_be_present:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:15

E

Error:
UserTest#test_should_be_valid:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:11

E

Error:
UserTest#test_email_validation_should_reject_invalid_addresses:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:44

E

Error:
UserTest#test_email_addresses_should_be_unique:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

rails test test/models/user_test.rb:53

E

Error:
SiteLayoutTest#test_layout_links:
ActiveRecord::Fixture::FormatError: fixture key is not a hash: /home/ubuntu/environment/sample_app/test/fixtures/users.yml, keys: ["name", "email", "password_digest"]

Error:
SiteLayoutTest#test_layout_links:
NoMethodError: undefined method `each' for nil:NilClass

rails test test/integration/site_layout_test.rb:5

<環境>
cloud9で開発

<実施したこと>
・全てのテストの内容を以下のgit内のと一致していることを確認しました。
https://github.com/yasslab/sample_apps/tree/master/6_0_0/ch08
・railsチュートリアル8章で書いたコードを一度コメントアウトしても同じエラーが出ました。

エラーがたくさん出てしまってとても驚いてしまい内容に不足点があるかもしれません…

<追記>
user.rb⇩

class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+-.]+@[a-z\d-.]+.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: true
has_secure_password
validates :password, presence: true, length: { minimum: 6 }

#渡された文字列のハッシュ値を返す
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end

yml

1 michael: 2 name: Michael Example 3 email: michael@example.com 4 password_digest: <%= User.digest('password') %>

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

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

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

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

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

Yasumichi

2020/05/22 14:35

app/models/user.rb を提示してください。 # 与えられた文字列のハッシュ値を返す あたりが怪しい気がします。
pome00000

2020/05/22 14:38

お忙しい中ご回答ありがとうございます!! 以下になります class User < ApplicationRecord before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true has_secure_password validates :password, presence: true, length: { minimum: 6 } # 渡された文字列のハッシュ値を返す def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end end
Yasumichi

2020/05/22 14:39

出来れば、質問に追記でお願いします。
Yasumichi

2020/05/22 14:46 編集

test/fixtures/users.yml の内容を以下のように質問に追記してください。 ```yml ここに内容を入れる ``` 上記により、インデントを含めて回答者に伝わります。
Yasumichi

2020/05/22 14:51 編集

rails g task fixtures check_format を実行するとフォーマットのチェックをしてくれるみたいです。 【追記】 rails fixtures:check_format を先ほどのコマンドの後に実行してください。
guest

回答1

0

ベストアンサー

はずしているかもしれませんが。

yml

1michael: 2 name: Michael Example 3 email: michael@example.com 4 password_digest: <%= User.digest('password') %>

のように michael: とそれ以外の行のインデントを変えてください。(michael: のインデントを解除してください。)

なお、 ActiveRecord::Fixture::FormatError: fixture key is not a hash をキーワードに検索したところ、以下のページが見つかりました。

ruby on rails - Resolving Active record fixture format error - Stack Overflow

おそらく、 ~/environment/sample_app/test/fixtures/users.yml のフォーマットに問題があります。上記ページについた回答によると

rails g task fixtures check_format

を実行すると lib/tasks/fixtures.rake ができるらしいです。

rails fixtures:check_format

投稿2020/05/22 15:00

編集2020/05/22 15:03
Yasumichi

総合スコア1773

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

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

pome00000

2020/05/22 15:05

ありがとうございます!!!! 解決しました!!とても丁寧にご指導いただけて 本当にありがたかったです!!
Yasumichi

2020/05/22 15:08

解決してよかったです。 yml というか、yaml はインデントが意味を持つので注意が必要です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問