Dave
Dave

Reputation: 1175

How can I correctly link to a static page?

I've followed a railscast on creating and displaying static pages using a pages model and my code looks like this:

Pages model

has fields of name, permalink and description.

Routes:

get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"

resources :pages

match ':permalink', :controller => 'pages', :action => 'show'

_footer.html.erb

<div class="footer">
  <div class="footer_container">
    <%= link_to 'About Us', root_path('about') %>
  </div>
</div>

going to /localhost:3000/about displays the about page correctly but the link in the footer wants to direct to /localhost:3000/.about and actually links to the sign up a new user page.

How can I get my link to direct to /about and display the page?

Thanks for any help its much appreciated!

Upvotes: 3

Views: 3540

Answers (1)

dpb
dpb

Reputation: 671

root_path will always take you to users#new because that is what you specified in your routes.rb file. What you can do is name your last route with the :as key, like this:

match ':permalink', :controller => 'pages', :action => 'show', :as => 'my_page'

Then in your views, you should be able to do something like this:

<%= link_to 'About Us', my_page_path('about') %>

Upvotes: 3

Related Questions