31

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 ?

user229044
  • 232,980
  • 40
  • 330
  • 338
geeky_monster
  • 8,672
  • 18
  • 55
  • 86
  • 4
    Instead of using `root_path`, use `redirect_to '/'` – user229044 Jun 06 '11 at 20:19
  • @meagar It's answer, not comment, so why don't you just create your answer instead of commenting on question? – MBO Jun 06 '11 at 20:21
  • 3
    @MBO It is not an answer. The question was "How do I do X?" and my comment was "Don't, do Y instead". This is not an acceptable answer IMO, and I would downvote others for posting it as such. – user229044 Jun 06 '11 at 20:23

5 Answers5

31

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • doing this also means that the view served has nothing to do with the other views in the app which is really unlikely. Views have a common layout, that's the beauty of Rails DRYness. – apneadiving Jun 06 '11 at 20:39
  • 7
    @apneadiving Doing so implies a lot more than just the lack of a shared layout, but there is nothing wrong with that. If you intend to serve a static index file then presumably you have decided there is *not* a common layout, and there is absolutely no reason not to serve `index.html` in this way. – user229044 Jun 06 '11 at 20:40
  • 1
    @meagar Totally. Actually, IMO it is a choice between _shared layout_ (MVC) and _quick response_ (static files). Rails supports both, the beauty of Rails flexibility. (In MVC case I can still use the CSS to keep a somewhat shared look.) – Franklin Yu Dec 21 '15 at 05:49
9

on controller

 redirect_to root_path ## (will redirect to root '/')
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
Rahul Patel
  • 1,386
  • 1
  • 14
  • 16
4

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

copremesis
  • 758
  • 7
  • 7
  • You actually don't even need to define the `def index` action. Only defining the controller and index file is enough. Rails will skip the action in the route, if it is not found. – LessQuesar Apr 03 '17 at 14:22
1

routes.rb

...
root to: redirect('public/index.html')
...

This will redirect all request to '/', to 'public/index.html'.

eriel marimon
  • 1,230
  • 1
  • 19
  • 29
1

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 :)

user229044
  • 232,980
  • 40
  • 330
  • 338
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • 8
    Rails supports serving static content from the `public` directory. This is completely "Rails compatible". – user229044 Jun 06 '11 at 20:20
  • 4
    @apneadiving Rails comes with a static `public/index.html` out of the box for every single new project, and it works pretty well. You might as well tell him he **must** touch the database or else he isn't using the 'M' in 'MVC'. – user229044 Jun 06 '11 at 20:24
  • 7
    Also "Rails is MVC, C for controller, V for view. So its internals need both" is completely false. Every time you do a `redirect_to` you're not using the "V" in MVC. – user229044 Jun 06 '11 at 20:27
  • @meagar I'm new to Rails (and MVC in general), so could you explain more why a `redirect_to` means we're not using the 'V' ? I don't get that. If that requires a new question for a long answer, tell me, I'll create it! – Lucas Jun 06 '11 at 20:40
  • @Lucas There is no *View* involved when you call `redirect_to` from a controller's action. – user229044 Jun 06 '11 at 20:42