Question

I'm newer to Rails and having an issue with my routes on a simple blog. I have the following routes below. When I navigate to example.com/posts then click on a post, everything seems to work fine. If I click on the Post link in my navbar to go back to example.com/post I end up with example.com/posts/posts. I've tried several different things from the Rails guide and just can't seem to figure this out. Thank you in advance for the assistance.

TestBed::Application.routes.draw do

resources :posts do
  resources :comments
end
root "pages#home"

get "/home", to: "pages#home", as: "home"

end

Edited to add navbar details

<li><%= link_to "Blog", "posts" %></li>
Was it helpful?

Solution

In your link_to helper you have "posts" as the URL. This appends the string posts to the end of the current url, which is the reason why you're seeing example.com/posts/posts.

To have the link properly point to /posts, modify your link_to call as follows:

<li><%= link_to "Blog", "/posts" %></li>

or use route helper:

<li><%= link_to "Blog", posts_path %></li>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top