Search code examples
ruby-on-railshyperlinkblogs

Linking to a post within Rails site from a Rails blog post produces posts/posts/article-name path


I'm a pretty new developer ~1 year, and I built a blogging site in Rails.

I have a set up where if I click on a post from the home page it goes to www.example.com/posts/article-name.

I decided for my Contact and About Me pages I would just make them blog posts, and then link to them from the nav bar using this syntax in my layouts/_navbar.html.erb file:

<li class="nav-item">
    <%= link_to 'About', "posts/about-me", class: "nav-link #{yield(:about_active)}" %>
</li>

When I click on the About link from the Home page it works fine, taking me to www.example.com/posts/about-me

However, if I am inside of a blog article and I click on the About page it takes me to www.example.com/posts/posts/about-me

I'm a little lost on how to fix this. I tried even hard-coding the link but that doesn't seem to work. Any help would be appreciated.


Solution

  • There's something you need to understand about absolute and relative paths.

    relative path is what you have tried.

    try the absolute path

    <%= link_to 'About', "/posts/about-me", class: "nav-link #{yield(:about_active)}" %>