0

public/index.html を使用して新しいホームページを実装したところ、多くのルートをオーバーライドすることがわかりました。

私は元々、ユーザーがログインしている場合、ログインしているホームページと、ログインしていない場合は匿名の訪問者 (public/index.html) のホームページを見ましたroot to: static_pages#homestatic_pages\home.html.erb

public/index.html を実装した後、ログインしているユーザー用の新しいルートを作成し、以前の root_path を home_path にリダイレクトしました。

get '/home' => 'static_pages#home', :as => :home

http://localhost:3000しかし、ログインホームページとビジターホームページの両方のホームページとして使用したいと考えています。これは可能ですか?訪問者に現在の public/index.html を表示させるにはどうすればよいhttp://localhost:3000/homeですか? http://localhost:3000ログイン後も欲しい。

ありがとう

4

2 に答える 2

1

public/index.htmlRailsの前にWebサーバーによって提供されるため、別のソリューションが必要です。

代わりに、次のようにコントローラーに移動public/index.htmlapp/views/static_pages/index.htmlて編集できます。

class StaticPagesController < ApplicationController
  def home 
    if signed_in?
      # current content of #home action
    else
      render :index, :layout => false
    end
  end
end

またはさらにきれいな方法

class StaticPagesController < ApplicationController
  before_filter :show_index_page, :only => :home

  def home 
    # the same
  end

private
  def show_index_page
    render :index, :layout => false unless signed_in?
  end
end
于 2013-05-04T00:44:37.520 に答える