简体   繁体   中英

Redirect “/” to resourceful routes in Rails' routes.rb

UPDATE (2018-Jan-11)

Based on suggestions, my files now look like the following. Behaviour remains the same (not working).

/rottenpotatoes/app/controllers/application_controller.rb :

class ApplicationController < ActionController::Base
  protect_from_forgery

  def index
      redirect_to root_url
  end
end

/rottenpotatoes/config/routes.rb :

Rottenpotatoes::Application.routes.draw do

  # map '/' to be a redirect to '/movies'
  root 'movies#index'
  resources :movies
end

I am trying to redirect the default/home directory on an app to "movies#show". I can't seem to manage it -- 'URL/movies' works fine, but according to the server logs, a request for / displays 'assets/rails.png' (the "welcome to Rails" page).

I'm sure there is a simple answer to this, I just don't know what it is. I have looked around SO and the Rails docs to no avail. I'm using c9.io, and running the server with rerun -- rails s -p $PORT -b $IP .

Rottenpotatoes/config/routes.rb:

Rottenpotatoes::Application.routes.draw do

  # map '/' to be a redirect to '/movies'
  get "/", to: "movies#index" # I also tried movies#show
  resources :movies
end

rake routes says:

RubyDep: WARNING: Your Ruby is outdated/buggy.
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.
RubyDep: WARNING: (To disable warnings, see:http://github.com/e2/ruby_dep/wiki/Disabling-warnings )
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
    Prefix Verb   URI Pattern                Controller#Action
           GET    /                          movies#index
    movies GET    /movies(.:format)          movies#index
           POST   /movies(.:format)          movies#create
 new_movie GET    /movies/new(.:format)      movies#new
edit_movie GET    /movies/:id/edit(.:format) movies#edit
     movie GET    /movies/:id(.:format)      movies#show
           PATCH  /movies/:id(.:format)      movies#update
           PUT    /movies/:id(.:format)      movies#update
           DELETE /movies/:id(.:format)      movies#destroy

How to I get "/" to go to "/movies"?

If you want to movies/index your default home page the try to the following in routes.rb

root 'movies#index'

The / is your root path if you use <a href="/"> Home </a> you will be redirected the home path

With the app method, you can access URL and path helpers, as well as do requests.

$ rails console
Loading development environment (Rails 5.0.6)
irb(main):001:0> app.root_path
=> "/"
irb(main):002:0> app.get _
Started GET "/" for 127.0.0.1 at 2018-01-11 12:24:38 ....

Hope to help

Please try

root 'movies#index'

Write it at the end of your routes.rb.

In routes.rb

root is used to define your root URL

And If its present more than once then it will be overridden by the last one.

Now after declaring root movies#index

You can use redirect_to root_url to redirect to /movies anywhere from your controllers

in routes.rb

Rottenpotatoes::Application.routes.draw do

  # map '/' to be a redirect to '/movies'
  root 'movies#index'
  resources :movies, except: [:index]
end   

and in controller where you want to redirect it

redirect root_path

Use this according to your code, this works:

Rottenpotatoes::Application.routes.draw do

  get "/", to: "movies#index", :as => "root" 
  resources :movies
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM