开发者

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 dire开发者_运维技巧ct 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!


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') %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜