简体   繁体   中英

How to change the home page of my Rails app to show Registration page instead?

I would like to make the homepage for an app (ie landing page) display a registration page. Unless the user is logged in - in which case they just find the "statuses" page.

Here are what I believe are the two relevant excerpts:

  as :user do
    get '/register', to: 'devise/registrations#new', as: :register    
    get '/login', to: 'devise/sessions#new', as: :login
    get '/logout', to: 'devise/sessions#destroy', as: :logout
  end

[...]

resources :statuses
  get 'feed', to: 'statuses#index', as: :feed
  root to: 'statuses#index'

Basically, I'm trying to get these two pages to swap their routes and route names. Unless, as mentioned, if someone is already signed in, then the landing page is the statuses page.

I only know a bit about this sort of thing, such as "get," and the URL aspects. If anyone could provide guidance, I'd be much obliged.

Here's the routes.rb file:

Treebook::Application.routes.draw do
  resources :activities, only: [:index]

  as :user do
    get '/register', to: 'devise/registrations#new', as: :register    
    get '/login', to: 'devise/sessions#new', as: :login
    get '/logout', to: 'devise/sessions#destroy', as: :logout
  end

  devise_for :users, skip: [:sessions]

  as :user do
    get "/login" => 'devise/sessions#new', as: :new_user_session
    post "/login" => 'devise/sessions#create', as: :user_session
    delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
  end

  resources :user_friendships do
    member do
      put :accept
      put :block
    end
  end

  resources :statuses
  get 'feed', to: 'statuses#index', as: :feed
  root to: 'statuses#index'

  scope ":profile_name" do
    resources :albums do
      resources :pictures
    end
  end

  get '/:id', to: 'profiles#show', as: 'profile'

end

Why not just send the user to the statuses page, and redirect all users who are not signed in to the sign in?

class StatusesController
  before_action :authorize!, only: :index

  def index
    #...
  end

  def authorize! # I believe this method is provided by devise
    unless signed_in?
      redirect_to new_user_session_path
    end
  end
end

# config/routes.rb
root 'statuses#index'

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