Naomi's notebook

Naomi's notebook

Ruby on Rails tutorial の通りにやってエラーが出るところとか

Ruby on Rails tutorial (https://railstutorial.jp/webtext) で、その通りにコマンドを実行してエラーが出る、かつ調べてもすぐに出てこないところとか、長めの演習の答えをメモしていきます。

ちなみに第6版を購入して利用しています。

第6章:heroku側でのデータベースのマイグレーションに失敗する・またはheroku環境でのコンソールを起動しようとしても動かない

一番最後にheroku環境でのコンソールを利用しようとすると

...
1: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:49:in `rescue in postgresql_connection'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:49:in `include?': no implicit conversion of nil into String (TypeError)

というエラーが出て起動しなかったことに気づきました。 これ実はデータベースのマイグレーションにも失敗しています。

Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

理由はデータベースのAdd-onを指定していないからです。 Herokuのアプリ管理ページに行き(必要ならログインしてください)、Configure Add-onsからHeroku Postgresを選んで追加しまます。この後マイグレーションし直すと成功します。 もしかしたらこれ、チュートリアルのアドオン追加の行を飛ばして読んでいただけかもしれません。(そうだったらすいません)

第14章:14.2.2 テストを書く演習

多分こんな感じです。 fixturesのrelationships.ymlで適当にmichael->archerのフォロー関係でも作っておきます。

one:
  follower_id: 1
  followed_id: 2

テストはこんな感じ

def setup
    @user = users(:michael)
    @followed_user = users(:archer)
    @not_followed_user = users(:lana)
  end

test "profile display" do
...
    #following,followers
    assert_select 'strong#following', text:@user.following.count.to_s
    assert_select 'strong#followers', text:@user.followers.count.to_s
    assert_select "a[href=?]", following_user_path(@user.id), count: 1
    assert_select "a[href=?]", followers_user_path(@user.id), count: 1
    #follow,unfollow button
    log_in_as(@user)
    get user_path(@followed_user)
    assert_select "input.btn-primary", value:"Follow"
    get user_path(@not_followed_user)
    assert_select "input.btn-primary", value:"Unfollow"
 end
end