見つけられるものはすべて読みましたが、まだ困惑しています。
before_filter を使用して、ログインしていないユーザーをキャッチし、別のインデックス ページに送信しようとしています。ログインしているユーザーが index.html.erb にアクセスすると、自分の記事の一覧が表示され、ログインしていないユーザーは、記事を一覧表示するが表示されない showall.html.erb ページにリダイレクトされます。それらを読んでもらいます(そして、いくつかの広告でヒットします)。
ルートを追加しました:
resources :articles do
get "showall"
resources :comments
end
「Rake routes」は、ルート article_showall を表示します。views/articles フォルダーに部分的な _showall.html.erb があります (「showall is working!」というテキストのみが含まれています)。別のビューで showall をレンダリングすると (<%= render "showall" %>)、問題なく動作します。
これは私のコントローラーの該当する部分です:
class ArticlesController < ApplicationController
skip_before_action :authorize, only: [:index, :show, :showall]
before_filter :require_user, :only => [:index]
def require_user
unless User.find_by(id: session[:user_id])
render 'showall', :notice => "Please log in to read articles."
end
end
def index
@articles = current_user.articles
end
def showall
@articles = Article.all
end
(ログインしていない状態で)実行すると、次のエラーが発生します。
Missing template articles/showall, application/showall with....etc
私は困惑しています。ビューで 'showall' をレンダリングできるのに、コントローラでそれを参照すると Missing Template エラーが発生するのはなぜですか? 助けてくれてありがとう!