Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Hiding URL in Rails?

I have looked around but haven't found a good answer to this question or one that I completely understood.

I have a basic app. It's going to be my web design site.

The question: When I go to localhost:3000 I get my root page.

Here is my routes.rb

Rails.application.routes.draw do
  get 'shared/home'
  get 'shared/about'
  get 'shared/contact'

  root 'shared#home'
end

I have "Home", "About", and "Contact" nav. They all work fine, I get the pages I want etc.

What I don't want to see is:

http://localhost:3000/shared/home
http://localhost:3000/shared/about
http://localhost:3000/shared/contact

I just want to see:

http://localhost:3000/home
http://localhost:3000/about
http://localhost:3000/contact

or just this:

http://localhost:3000

How do you hide the folder structure from the URL bar and the status bar?

2 Answers

Dylan Shine
Dylan Shine
17,565 Points

Hi Scott,

Just a minor error, you're naming your routes wrong.

Example: get '/about', to: 'ControllerName#ControllerMethod' (Replace the controller name and method accordingly)

Hope that helps, Dylan

Its working now. Thank you.

This is the controller.

class SharedController < ApplicationController
  def home
  end

  def about
  end

  def contact
  end
end

This is the new routes.rb

Rails.application.routes.draw do
  get '/home', to: 'shared#home'
  get '/about', to: 'shared#about'
  get '/contact', to: 'shared#contact'

  root 'shared#home'

This is the code in the _header.html.erb

<div class="collapse navbar-collapse" id="top-nav-collapse">
    <ul class="nav navbar-nav navbar-right">
        <li><%=nav_link("Home", home_path) %></li>
        <li><%=nav_link("About", about_path) %></li>
        <li><%=nav_link("Contact", contact_path) %></li>
    </ul>
</div>