How do I redirect to root - public/index.html?

2019-03-17 09:34发布

I wish to do a redirection to index.html in my application/public folder.

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 )

 # root :to => "welcome#index"

I get an error saying root_path is undefined.

How do I modify routes.rb so that root_path points to public/index.html ?

4条回答
Bombasti
2楼-- · 2019-03-17 10:13

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

查看更多
Explosion°爆炸
3楼-- · 2019-03-17 10:14

What you want to do is not Rails compatible.

Rails is MVC, C for controller, V for view.

So its internals need both.

Ok, public/index.html is displayed by default but it's just because process is bypassed.

So, you could create a static controller with an index action and it's corresponding view (just copy/paste the content of your current public/index.htmlfile in it).

Then set:

root :to => "static#index"

And please, remove the public/index.html file :)

查看更多
仙女界的扛把子
4楼-- · 2019-03-17 10:25

on controller

 redirect_to root_path (will redirect to root '/')
查看更多
手持菜刀,她持情操
5楼-- · 2019-03-17 10:26

You can assign a named route to a static file by passing any non-empty string as :controller and the path to the file as the :action for the route:

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.

查看更多
登录 后发表回答