###前提・実現したいこと
rails4 ローカル環境
deviseを使用してログイン機能を実装しました。
そこで、トップページとなるtop/inedex.html.erbで<% if user_signed_in? %>を使い、
ログインしてない場合はログイン用のページ
ログインしている場合は自分のトップページ
となるようにしました。
ログインしている場合のトップページで自分のタイムラインを表示して、kaminariを導入してページネーション機能をつけました。
###発生している問題・エラーメッセージ
ページネーション機能をつけて、ログイン時は正常に表示されるのですが、ログアウトすると以下のようなエラーになってしまいます。
NoMethodError at / undefined method `page' for nil:NilClass
###該当のソースコード
top_controller.rb
class TopController < ApplicationController def index @timelines = current_user.try(:timeline).page(params[:page]).per(25).order(created_at: :desc) end end
views/top/index.html.erb
<% if user_signed_in? %> 省略 <h4>タイムライン</h4> <% @timelines.each do |timeline| %> <div class="row timeline-list"> <%= link_to user_path(timeline.user_id) do %> <%= profile_img(timeline.user) %> <% end %> <p class="name"><b><%= timeline.user.name %></b><span class="right-list"><%= time_ago_in_words timeline.created_at %>前</span></p> <%= link_to timeline_path(timeline) do %> <p class="timeline-content"><%= timeline.content %></p> <% end %> </div> <% end %> <%= paginate(@timelines) %> <% else %> 省略 <%= link_to 'ログイン', new_user_session_path %> <%= link_to 'サインアップ', new_user_registration_path %> <% end %>
ログアウト時に、page
がnil
となってしまうためエラーになっていると解釈しているのですが、どのように解決すればいいのかが分かりません。
どう修正すればいいか教えて頂ける嬉しいです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/03 06:34
2017/04/03 06:46
2017/04/03 10:43
2017/04/03 10:46