Link to root path in rails

14,614

Solution 1

You can use root_path to go back to home page.

Change the below line on your code with root_path:

<center><b style="font-size: 40px"><%= link_to "AucIt", root_path %></b>  </center>

Solution 2

You generated relative links, but you need to use absolute links.

A relative link does not start with a /. Imagine you are on a page /foo and you have that relative link bar/baz, then you would end up on /foo/bar/baz. If you want to go to /bar/baz instead then your links have to be absolute links – starting with a /.

<center><b style="font-size: 40px"><%= link_to "AucIt", '/pages/home' %></b></center>
<nav>   
  <ul>
    <li style="font-size:20px"><a href="/">Sign Up</a></li>
    <li style="font-size:20px"><%= link_to "Login", '/static_pages/login' %></li>
    <li style="font-size:20px"><%= link_to "List", '/static_pages/list' %></li>
  </ul>
</nav>

Furthermore, I suggest using the Rails link helpers and not to use inline CSS styles. The following is just an example because CSS and the routing depend highly on your needs and your app.

# your view
<%= link_to 'AudIt', pages_path(id: 'home'), class: 'home-link' %>
<nav>
  <ul>
    <li><%= link_to 'Sign Up', root_path %></li>
    <li><%= link_to 'Login', static_page_path(id: 'login') %></li>
    <li><%= link_to 'List', static_page_path(id: 'list') %></li>
  </ul>
</nav>

# example routes.rb
root to: 'controller#action'
resources :pages, only: [:show]
resources :static_pages, only: [:show]

# example CSS
home-link {
  display: block;
  font-size: 40px;
  text-align: center;
  text-weigth: bold;
}
li {
  font-size: 20px;
}
Share:
14,614
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am creating a Ruby on Rails website application. I created a couple of other links on my website but this one isn't working properly.

    I want to create a link on all the pages that will send the user back to the home page when they click on it.

    home.html.erb:

    <center><b style="font-size: 40px"><%= link_to "AucIt", 'pages/home' %></b>  </center>
    <nav>   
    <ul>
       <li style="font-size: 20px"><a href="">Sign Up</a></li>
       <li style="font-size:20px"><%= link_to "Login",   'static_pages/login' %></li>
       <li style="font-size:20px"><%= link_to "List",   'static_pages/list' %> </li>
    
    </ul>
    
    </nav>
    

    The list and login links works fine but the "aucit" link doesn't work.

  • Admin
    Admin about 8 years
    I tried before and it didn't work but it works now . Thanks so much