LoginSignup
0
0

More than 1 year has passed since last update.

railsチュートリアル第十四章[Following]と[Followers]ページ

Posted at

[Following]と[Followers]ページ

どちらにもフォローの統計情報などのユーザー情報を表示するサイドバーと、ユーザーのリスト
サイドバーには小さめのユーザープロフィール画像のリンクを格子状に並べて表示する予定です。

フォロー/フォロワーページの認可をテストする

test/controllers/users_controller_test.rb

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  def setup
    @user       = users(:michael)
    @other_user = users(:archer)
    # テストユーザー
  end
.
.
.
  test "should redirect following when not logged in" do
    get following_user_path(@user)
    # フォローの数を表示を要求
    assert_redirected_to login_url
    # ログインフォームに移動するか?
  end

  test "should redirect followers when not logged in" do
    get followers_user_path(@user)
    # フォワーの数を表示を要求
    assert_redirected_to login_url
  end
end

followingアクションとfollowersアクション

app/controllers/users_controller.rb

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy
  # before_actionメソッドを使って何らかの処理が実行される直前に
  # 特定のメソッドを実行する仕組み
  # ログインをさせる
  # :editと:updateアクションだけ
.
.
.
  def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following.paginate(page: params[:page])
    render 'show_follow'
    # show_follow.html.erbへ移動させる
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
    # show_follow.html.erbへ移動させる
  end
.
.
.
end

フォローしているユーザーとフォロワーの両方を表示するshow_followビュー

app/views/users/show_follow.html.erb

<% provide(:title, @title) %>
<div class="row">
  <aside class="col-md-4">
    <section class="user_info">
      <%= gravatar_for @user %>
      <h1><%= @user.name %></h1>
      <!--名前-->
      <span><%= link_to "view my profile", @user %></span>
      <!--リンク メッセージ リンク先-->
      <span><b>Microposts:</b> <%= @user.microposts.count %></span>
    </section>
    <section class="stats">
      <%= render 'shared/stats' %>
      <% if @users.any? %>
        <div class="user_avatars">
          <% @users.each do |user| %>
            <%= link_to gravatar_for(user, size: 30), user %>
          <% end %>
        </div>
      <% end %>
    </section>
  </aside>
  <div class="col-md-8">
    <h3><%= @title %></h3>
    <% if @users.any? %>
      <ul class="users follow">
        <%= render @users %>
      </ul>
      <%= will_paginate %>
    <% end %>
  </div>
</div>
テスト
ubuntu:~/environment/sample_app (following-users) $ rails t
Running via Spring preloader in process 8412
Started with run options --seed 21083

  65/65: [===========================] 100% Time: 00:00:06, Time: 00:00:06

Finished in 6.54711s
65 tests, 322 assertions, 0 failures, 0 errors, 0 skips

統合テスト作成

ubuntu:~/environment/sample_app (following-users) $ rails generate integration_test following
Running via Spring preloader in process 8684
      invoke  test_unit
      create    test/integration/following_test.rb
      # 統合テスト 生成

following/followerをテストするためのリレーションシップ用fixture

test/fixtures/relationships.yml

one:
  follower: michael
  # フォロワー
  followed: lana
  # フォローされている人

two:
  follower: michael
  followed: malory

three:
  follower: lana
  followed: michael

four:
  follower: archer
  followed: michael

following/followerページのテスト

test/integration/following_test.rb

require 'test_helper'

class FollowingTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
    # テストユーザー
    log_in_as(@user)
    # ログインさせる
  end

  test "following page" do
    # followページに正確にフォッロワーが表示しているかどうかのテスト
    get following_user_path(@user)
    # ユーザーのフォロー数を表示させることを要求
    assert_not @user.following.empty?
    # ユーザーのフォローが0でないか確認?
    assert_match @user.following.count.to_s, response.body
    # フォロー数がユーザーページにあるかどうか確認?
    @user.following.each do |user|
    # ユーザーのフォローしているユーザー数を一つづつ取り出す
      assert_select "a[href=?]", user_path(user)
      # ひとつずつ取り出してaタグにユーザーが書かれているか?
    end
  end

  test "followers page" do
    get followers_user_path(@user)
    assert_not @user.followers.empty?
    assert_match @user.followers.count.to_s, response.body
    @user.followers.each do |user|
      assert_select "a[href=?]", user_path(user)
    end
  end
end
テスト
ubuntu:~/environment/sample_app (following-users) $ rails tRunning via Spring preloader in process 10581
Started with run options --seed 6735

  67/67: [============================] 100% Time: 00:00:07, Time: 00:00:07

Finished in 7.57755s
67 tests, 332 assertions, 0 failures, 0 errors, 0 skips

演習

1.
ブラウザから /users/1/followers と /users/1/following を開き、それぞれが適切に表示されていることを確認してみましょう。サイドバーにある画像は、リンクとしてうまく機能しているでしょうか?

確認

2.
リスト 14.29のassert_selectに関連するコードをコメントアウトしてみて、テストが正しく red に変わることを確認してみましょう。

わからない。

困ったこと

MiniMagick::Error in ActiveStorage::RepresentationsController#show
You must have ImageMagick or GraphicsMagick installed
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0