# トップページが複数の挙動をする場合にどう書くか

**URL:** <https://discourse.clean-rails.org/t/topic/147>\
**Category:** Uncategorized\
**Created:** [2018 年 8 月 26 日午前 9:06 UTC](https://discourse.clean-rails.org/t/topic/147 "2018-08-26T09:06:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![willnet](https://discourse.clean-rails.org/user_avatar/discourse.clean-rails.org/willnet/32/4_2.png) [@willnet](https://discourse.clean-rails.org/u/willnet)\
**Post date:** [2018 年 8 月 26 日午前 9:06 UTC](https://discourse.clean-rails.org/t/topic/147/1 "2018-08-26T09:06:09Z")

</div>

[場合分けのあるアクションをどのように綺麗に書くか](https://discourse.clean-rails.org/t/topic/134) と関連してますが。

webサービスのルート`/`を表示するとき、

- 未ログインなら通常のトップページ
- ログイン済みならマイページ

のようになるwebサービスは結構あると思います。facebookやtwitterなどが該当しますね。

これをrailsでふつうに実装すると、単一のアクションにまったく別の二種類のロジックが含まれることになります。

```auto
class WelcomeController < ApplicationController
  skip_before_action :authenticate

  def index
    unless logged_in?
      # トップページ用のロジック
      return
    end
    
    # マイページ用のロジック    
    render 'mypage'
  end
end

```

上記は単純な例なので二種類ですが、場合によっては三種類以上の画面を出し分けたいケースもあるかもしれません。

これ、もっと綺麗にかける方法はないかと考えているのですが良い方法知っている人いませんか。
