Update the routes


Here are the final tweaks to be made before we can go live and show the world our brand new applications!

Making the new page a landing page

After you've finished customizing your landing page, you probably want to make it the default page your users see. To do this, replace the line from config/routes.rb using instead of:

root to: "posts#index"

the following code:

root to: "pages#landing"

Now just go to http://localhost:3000 and your new landing page will greet you!

Who sees what?

If you configured registration and sign in buttons on your landing page, you may feel confused on how the actually work, since it seems that users do not manage to sign in.

Actually, what happens is that if you're already authenticated, you see the landing page, and when you try to sign in... you are redirected to the same page.

We'd like a different logic here.

So:

  • If the user is not signed in, when they go to the root of the website (localhost:3000), they should see the landing page
  • If the user is signed in, when they go to the root of the website, they should see the Posts page.

Remember, we have a before filter in the Posts controller that prevents unauthorized access to the Posts Index page.

To implement this logic, we need to change in config/routes.rb the previous root to: pages#landing to a more complex structure:

authenticated :user do
  root to: "posts#index", as: :authenticated_root
end

root to: "pages#landing"

This tells Rails that authenticated users will see the Index view of Posts and everyone else will see the Landing view of Pages.

Finally, this is the whole content of my config/routes.rb file:

Rails.application.routes.draw do
  devise_for :users

  resources :posts

  authenticated :user do
    root to: "posts#index", as: :authenticated_root
  end

  root to: "pages#landing"

  get "landing" => "pages#landing"
end

results matching ""

    No results matching ""