Ruby-on-rails 如何重定向到 root - public/index.html?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6257468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I redirect to root - public/index.html?
提问by geeky_monster
I wish to do a redirection to index.html in my application/public folder.
我希望重定向到我的 application/public 文件夹中的 index.html。
def get_current_user
@current_user = current_user
if @current_user.nil?
redirect_to root_path
end
end
How do I achieve this ?
我如何实现这一目标?
I haven't modified the root in my routes.rb ( Its still commented )
我没有修改我的 routes.rb 中的根目录(它仍然被评论)
# root :to => "welcome#index"
I get an error saying root_path is undefined.
我收到一条错误消息,说 root_path 未定义。
How do I modify routes.rb so that root_path points to public/index.html ?
如何修改 routes.rb 以便 root_path 指向 public/index.html ?
采纳答案by apneadiving
What you want to do is not Rails compatible.
您想要做的是与 Rails 不兼容。
Rails is MVC, C for controller, V for view.
Rails 是 MVC,C 代表控制器,V 代表视图。
So its internals need both.
所以它的内部结构需要两者。
Ok, public/index.htmlis displayed by default but it's just because process is bypassed.
好的,public/index.html默认显示,但这只是因为绕过了进程。
So, you could create a staticcontroller with an indexaction and it's corresponding view (just copy/paste the content of your current public/index.htmlfile in it).
因此,您可以创建一个static带有index动作的控制器及其对应的视图(只需将当前public/index.html文件的内容复制/粘贴到其中)。
Then set:
然后设置:
root :to => "static#index"
And please, remove the public/index.htmlfile :)
请删除public/index.html文件:)
回答by meagar
You can assign a named route to a static file by passing any non-empty string as :controllerand the path to the file as the :actionfor the route:
您可以通过传递任何非空字符串 as: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 /
Assuming you have a public/index.html, this is what will be served.
假设您有一个public/index.html,这就是将要提供的内容。
回答by Rahul Patel
on controller
在控制器上
redirect_to root_path ## (will redirect to root '/')
回答by copremesis
route file:
路由文件:
root 'main#index'
controller:
控制器:
class MainController < ApplicationController
def index
redirect_to '/index.html'
end
end
and using rails 4 controller action live this can behave like a single page application using the M & C with a twist on the V
并使用 rails 4 控制器动作,这可以像使用 M & C 的单页应用程序一样运行,并在 V 上进行扭曲
回答by eriel marimon
routes.rb
路由文件
...
root to: redirect('public/index.html')
...
This will redirect all request to '/', to 'public/index.html'.
这会将所有请求重定向到'/', 到'public/index.html'。

