All threads / Link_to error

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Link_to error

Simon P asked in Rails

Hi

I have my header in a partial which then appears on every page through the application template.

In the header I have an About link:

<%= link_to "About", 'about' %>

This works fine in most of the site.

But when I am in www.oilersdaily.com/articles it links to www.oilersdaily.com/articles/about rather than www.oilersdaily.com/about

about.html.erb is in the folder static_pages and my routes.rb has :

get 'about', to: 'static_pages#about'

Any guidance appreciated, a schoolboy error I am sure!!

Thanks

Simon

Your current link will be a relative link. If you do <%= link_to "About", about_path %> it should work as expected. All links defined in your routes file can be found through rake routes (or rails routes). "Simply" append _path to each link from the first column to get every correct url within your app.
Also if you want to link from an external source, eg. mailer, you can append these with _url.

This resource might be useful: http://guides.rubyonrails.org/routing.html

Thanks

That Routing page is on my reading list already!!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.