我刚刚使用public / index.html实现了一个新主页,我读到它覆盖了很多路由。
我最初有root to: static_pages#home
,如果用户已登录,则在我的static_pages\home.html.erb
视图中,如果没有登录,他们会看到已登录的主页和匿名访客(public / index.html)主页。
在实现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
。
由于
答案 0 :(得分:1)
public/index.html
由Rails之前的网络服务器提供,因此您需要另一种解决方案。
相反,您可以将public/index.html
移至app/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
答案 1 :(得分:0)
您需要的是一个简单的HomeController,它为相应的用户呈现正确的视图。
您必须拥有匿名和登录用户的两个erb视图文件。