如何重定向到root – public / index.html?

我希望在我的应用程序/公共文件夹中重定向到index.html。

def get_current_user @current_user = current_user if @current_user.nil? redirect_to root_path end end 

我该如何实现这一目标?

我没有修改我的routes.rb中的根(它仍然评论)

  # root :to => "welcome#index" 

我收到一个错误,说root_path未定义。

如何修改routes.rb以便root_path指向public / index.html?

你想要做的不是Rails兼容。

Rails是MVC,C代表控制器,V代表视图。

所以它的内部需要两者兼而有之。

好的,默认显示public/index.html ,但这只是因为绕过了进程。

因此,您可以使用index操作创建一个static控制器,并使用相应的视图(只需复制/粘贴当前public/index.html文件的内容)。

然后设置:

 root :to => "static#index" 

请删除public/index.html文件:)

您可以将命名路由分配给静态文件,方法是将任何非空字符串作为:controller和文件路径作为路径的:action传递:

 Application.routes.draw do root :controller => 'static', :action => '/' # or # root :controller => 'static', :action => '/public/index.html' end # elsewhere redirect_to root_path # redirect to / 

假设你有一个public/index.html ,这将是服务的。

在控制器上

  redirect_to root_path (will redirect to root '/') 

路线文件:

  root 'main#index' 

控制器:

  class MainController < ApplicationController def index redirect_to '/index.html' end end 

并且使用rails 4控制器动作实时这可以表现得像使用M&C并在V上扭曲的单页应用程序